---
# System prepended metadata

title: 'LeetCode #965 [Univalued Binary Tree](https://leetcode.com/problems/univalued-binary-tree/)'
tags: [Recursion, LeetCode, Easy, Tree]

---

###### tags: `LeetCode` `Tree` `Easy` `Recursion`
# LeetCode #965 [Univalued Binary Tree](https://leetcode.com/problems/univalued-binary-tree/)
### (Easy)
如果二元樹每個節點都具有相同的值，那麼該二元樹就是單值二元樹。

只有給定的樹是單值二元樹時，才返回 true；否則返回 false。


---
比對節點的值與root的值是否相等, 不相等回傳否, 相等則繼續比對左右子節點, 直到節點為null, 回傳true。


---
```
class Solution {
public:
    bool isUnivalTree(TreeNode* root) {
        return check(root,root->val);
    }
    bool check(TreeNode* node, int val){
        if(!node)return true;
        return (node->val==val)&&check(node->left,val)&&check(node->right,val);
    }
};
```