Created
August 24, 2017 15:20
-
-
Save cixuuz/8e8ecf2b822dcf03d97ef31d6b33ce2d to your computer and use it in GitHub Desktop.
[250. Count Univalue Subtrees] #leetcode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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