-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCountUnivalueSubtrees.java
56 lines (47 loc) · 1.39 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
50
51
52
53
54
55
56
package techQuestions;
/*
Purpose: determining count of univalue subtrees
Author: Erich Meissner
Date: 5/5/20
Time: 9:35 PM
*/
public class CountUnivalueSubtrees {
public static void main(String[] args) {
TreeNode root = new TreeNode(5);
root.left = new TreeNode(1);
root.right = new TreeNode(5);
root.left.left = new TreeNode(5);
root.left.right = new TreeNode(5);
root.right.right = new TreeNode(5);
System.out.println(countUnivalSubtrees(root));
}
public static int count = 0;
public static int countUnivalSubtrees(TreeNode root) {
if (root == null) {
return 0;
}
isUnival(root);
return count;
}
public static boolean isUnival(TreeNode root) {
// base case for child node
if (root.left == null && root.right == null) {
count++;
return true;
}
// initialize is univalue boolean to be true
boolean isUnival = true;
if (root.left != null) {
isUnival = isUnival(root.left) && root.val == root.left.val;
}
if (root.right != null) {
isUnival = isUnival(root.right) && isUnival && root.val == root.right.val;
}
// if the root at this point is not univalued
if (!isUnival) {
return false;
}
count++;
return true;
}
}