Skip to content

Commit 2e9d13a

Browse files
solves univalues binary tree
1 parent 2de43ca commit 2e9d13a

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@
260260
| 949 | [Largest Time for Given Digits](https://leetcode.com/problems/largest-time-for-given-digits) | |
261261
| 953 | [Verifying an Alien Dictionary](https://leetcode.com/problems/verifying-an-alien-dictionary) | [![Java](assets/java.png)](src/VerifyAnAlienDictionary.java) |
262262
| 961 | [N-Repeated Elements in Size 2N Array](https://leetcode.com/problems/n-repeated-element-in-size-2n-array) | [![Java](assets/java.png)](src/NRepeatedElementInSizeNArray.java) |
263-
| 965 | [Univalued Binary Tree](https://leetcode.com/problems/univalued-binary-tree) | |
263+
| 965 | [Univalued Binary Tree](https://leetcode.com/problems/univalued-binary-tree) | [![Java](assets/java.png)](src/UnivaluedBinaryTree.java) |
264264
| 970 | [Powerful Integers](https://leetcode.com/problems/powerful-integers) | |
265265
| 976 | [Largest Perimeter Triangle](https://leetcode.com/problems/largest-perimeter-triangle) | |
266266
| 977 | [Squares of a Sorted Array](https://leetcode.com/problems/squares-of-a-sorted-array) | |

src/UnivaluedBinaryTree.java

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
public class UnivaluedBinaryTree {
2+
private int val;
3+
4+
public boolean isUnivalTree(TreeNode root) {
5+
val = root.val;
6+
return isUnival(root);
7+
}
8+
9+
private boolean isUnival(TreeNode root) {
10+
if (root == null) return true;
11+
if (root.val != val) return false;
12+
return isUnival(root.left) && isUnival(root.right);
13+
}
14+
}

0 commit comments

Comments
 (0)