Skip to content

Commit 0655eda

Browse files
add 965
1 parent 15dc44a commit 0655eda

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Your ideas/fixes/algorithms are more than welcome!
2929

3030
| # | Title | Solutions | Time | Space | Video | Difficulty | Tag
3131
|-----|----------------|---------------|---------------|---------------|--------|-------------|-------------
32+
|965|[Univalued Binary Tree](https://leetcode.com/problems/univalued-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_965.java) | O(n) | O(h) | |Easy| DFS, recursion
3233
|961|[N-Repeated Element in Size 2N Array](https://leetcode.com/problems/n-repeated-element-in-size-2n-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_961.java) | O(n) | O(1) | |Easy|
3334
|944|[Delete Columns to Make Sorted](https://leetcode.com/problems/delete-columns-to-make-sorted/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_944.java) | O(n) | O(1) | |Easy|
3435
|941|[Valid Mountain Array](https://leetcode.com/problems/valid-mountain-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_941.java) | O(n) | O(1) | |Easy|
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.fishercoder.solutions;
2+
3+
import com.fishercoder.common.classes.TreeNode;
4+
5+
/**
6+
* 965. Univalued Binary Tree
7+
*
8+
* A binary tree is univalued if every node in the tree has the same value.
9+
*
10+
* Return true if and only if the given tree is univalued.
11+
*
12+
* Example 1:
13+
*
14+
* 1
15+
* / \
16+
* 1 1
17+
* / \ \
18+
* 1 1 1
19+
*
20+
* Input: [1,1,1,1,1,null,1]
21+
* Output: true
22+
*
23+
*
24+
* Example 2:
25+
* 2
26+
* / \
27+
* 2 2
28+
* / \
29+
* 5 2
30+
*
31+
* Input: [2,2,2,5,2]
32+
* Output: false
33+
*
34+
*
35+
* Note:
36+
*
37+
* The number of nodes in the given tree will be in the range [1, 100].
38+
* Each node's value will be an integer in the range [0, 99].
39+
*/
40+
public class _965 {
41+
public static class Solution1 {
42+
public boolean isUnivalTree(TreeNode root) {
43+
if (root == null) {
44+
return true;
45+
}
46+
return dfs(root, root.val);
47+
}
48+
49+
private boolean dfs(TreeNode root, int value) {
50+
if (root == null) {
51+
return true;
52+
}
53+
if (root.val != value) {
54+
return false;
55+
}
56+
return dfs(root.left, value) && dfs(root.right, value);
57+
}
58+
}
59+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.common.classes.TreeNode;
4+
import com.fishercoder.common.utils.TreeUtils;
5+
import com.fishercoder.solutions._14;
6+
import com.fishercoder.solutions._965;
7+
import java.util.Arrays;
8+
import java.util.List;
9+
import org.junit.BeforeClass;
10+
import org.junit.Test;
11+
12+
import static org.junit.Assert.assertEquals;
13+
14+
public class _965Test {
15+
private static _965.Solution1 solution1;
16+
private static TreeNode root;
17+
18+
@BeforeClass
19+
public static void setup() {
20+
solution1 = new _965.Solution1();
21+
}
22+
23+
@Test
24+
public void test1() {
25+
root = TreeUtils.constructBinaryTree(Arrays.asList(1, 1, 1, 1, 1, null, 1));
26+
assertEquals(true, solution1.isUnivalTree(root));
27+
}
28+
29+
@Test
30+
public void test2() {
31+
root = TreeUtils.constructBinaryTree(Arrays.asList(2, 2, 2, 5, 2));
32+
assertEquals(false, solution1.isUnivalTree(root));
33+
}
34+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy