Coding Test/LeetCode
[LeetCode] 965. Univalued Binary Tree c++
owls
2022. 12. 22. 13:52
728x90
- 문제
A binary tree is uni-valued if every node in the tree has the same value.
Given the root of a binary tree, return true if the given tree is uni-valued, or false otherwise.
Input: root = [1,1,1,1,1,null,1]
Output: true
tree node들은 모두 같은 값인지 확인하는 문제이다.
- 문제 해결
class Solution {
private:
bool Recur(TreeNode* curr, int nValue){
if(!curr){
return true;
}
if(curr->val != nValue){
return false;
}
return Recur(curr->left, nValue) && Recur(curr->right, nValue);
}
public:
bool isUnivalTree(TreeNode* root) {
int n = root->val;
return Recur(root, n);
}
};
728x90