forked from oleg-cherednik/DailyCodingProblem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
60 lines (46 loc) · 1.34 KB
/
Solution.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
57
58
59
60
/**
* @author Oleg Cherednik
* @since 07.12.2018
*/
public class Solution {
public static void main(String... args) {
Node node = createTree();
System.out.println(countUnivalSubtrees(node));
}
private static Node createTree() {
Node a = new Node(1);
a.left = new Node(1);
a.right = new Node(1);
Node b = new Node(0);
b.left = a;
b.right = new Node(0);
Node root = new Node(0);
root.left = new Node(1);
root.right = b;
return root;
}
private static class Node {
private final int val;
private Node left;
private Node right;
public Node(int val) {
this.val = val;
}
}
public static int countUnivalSubtrees(Node node) {
if (node == null)
return 0;
int count = countUnivalSubtrees(node.left);
count += countUnivalSubtrees(node.right);
return isUnivalTree(node) ? count + 1 : count;
}
private static boolean isUnivalTree(Node node) {
if (node == null)
return true;
if (node.left != null && node.left.val != node.val)
return false;
if (node.right != null && node.right.val != node.val)
return false;
return isUnivalTree(node.left) && isUnivalTree(node.right);
}
}