Skip to content

Instantly share code, notes, and snippets.

@cixuuz
Created August 24, 2017 15:20
Show Gist options
  • Select an option

  • Save cixuuz/8e8ecf2b822dcf03d97ef31d6b33ce2d to your computer and use it in GitHub Desktop.

Select an option

Save cixuuz/8e8ecf2b822dcf03d97ef31d6b33ce2d to your computer and use it in GitHub Desktop.
[250. Count Univalue Subtrees] #leetcode
class Solution {
// O(n) O(n)
private int count = 0;
public int countUnivalSubtrees(TreeNode root) {
unival(root, 0);
return count;
}
private boolean unival(TreeNode node, int val) {
if (node == null) return true;
if (!unival(node.left, node.val) | !unival(node.right, node.val)) {
return false;
}
count++;
return node.val == val;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment