-
Notifications
You must be signed in to change notification settings - Fork 0
/
Count Univalue Subtrees.java
53 lines (46 loc) · 1.26 KB
/
Count Univalue Subtrees.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
/*
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.
Link: https://leetcode.com/problemset/algorithms/
Example:
Given binary tree,
5
/ \
1 5
/ \ \
5 5 5
return 4.
Solution: None
Source: http://blog.csdn.net/pointbreak1/article/details/48998407
*/
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int countUnivalSubtrees(TreeNode root) {
unival(root);
return count;
}
private boolean unival(TreeNode root) {
if (root == null)
return true;
if (root.left == null && root.right == null) {
count++;
return true;
}
boolean left = unival(root.left);
boolean right = unival(root.right);
if (left && right && (root.left == null || root.left.val == root.val) && (root.right == null || root.right.val == root.val)) {
count++;
return true;
}
return false;
}
private int count = 0;
}