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.
For example:
Given binary tree,
1 2 3 4 5
| 5 / \ 1 5 / \ \ 5 5 5
|
return 4
.
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
| public class CountUnivalueSubtrees {
public int countUnivalSubtrees(TreeNode root) { if (root == null) { return 0; }
if (root.left == null && root.right == null) { return 1; }
int[] result = new int[]{0}; helper(root, result); return result[0]; }
private boolean helper(TreeNode root, int[] result) {
if (root.right == null && root.left == null) { result[0]++; return true; } else if (root.right != null && root.left == null) { if (helper(root.right, result) && root.val == root.right.val) { result[0]++; return true; } else { return false; } } else if (root.right == null) { if (helper(root.left, result) && root.val == root.left.val) { result[0]++; return true; } else { return false; } } else { boolean l = helper(root.right, result); boolean r = helper(root.left, result); if (l && r && root.val == root.left.val && root.val == root.right.val) { result[0]++; return true; } else { return false; } }
} }
|