-
[LeetCode] 965. Univalued Binary Tree c++Coding Test/LeetCode 2022. 12. 22. 13:52728x90
- 문제
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'Coding Test > LeetCode' 카테고리의 다른 글
[LeetCode] 145. Binary Tree Postorder Traversal c++ (0) 2022.12.22 [LeetCode] 872. Leaf-Similar Trees c++ (0) 2022.12.22 [LeetCode] 463. Island Perimeter c++ (0) 2022.12.22 [LeetCode] 559. Maximum Depth of N-ary Tree c++ (0) 2022.12.20 [LeetCode] 637. Average of Levels in Binary Tree c++ (0) 2022.12.20