forked from armankhondker/leetcode-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
countUnivalueSubtrees.java
49 lines (38 loc) · 1.36 KB
/
countUnivalueSubtrees.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
Given a binary tree, count the number of uni-value subtrees.
A Uni-value subtree means all nodes of the subtree have the same value.
Example :
Input: root = [5,1,5,5,5,null,5]
5
/ \
1 5
/ \ \
5 5 5
Output: 4
Go through the etnire tree, in a bottom up fashion, checking if the tree is unival
TC: O(N) will visit every node exactly once
SC: O(N) for the recursive stack that will grow to at most N
public class Solution {
public int countUnivalSubtrees(TreeNode root) {
int[] count = new int[1]; //pass by reference in javas
helper(root, count);
return count[0];
}
private boolean helper(TreeNode node, int[] count) {
if (node == null) {
return true;
}
boolean left = helper(node.left, count); //check if left subtree is unival
boolean right = helper(node.right, count); //check if right subtree is unival
if (left && right) { //if left tree and right tree are unival, then check the root val with left and right
if (node.left != null && node.val != node.left.val) {
return false;
}
if (node.right != null && node.val != node.right.val) {
return false;
}
count[0]++;
return true;
}
return false;
}
}