Univalued Binary Tree
给一个树, 看是否值全部相同. 直接遍历就好, 注意返回的时候求一个&&
1 2 3 4 5 6 7 8 9 10 11 12 |
class Solution { int t = Integer.MAX_VALUE; public boolean isUnivalTree(TreeNode root) { if(root == null) // if null, return true. return true; if(t == Integer.MAX_VALUE) // if first time, set t t = root.val; else if(t != root.val) // if find the diff, return false return false; return isUnivalTree(root.left) && isUnivalTree(root.right); } } |