Created
January 1, 2019 09:14
-
-
Save fpdjsns/7a0ca7edb3d312c4e587ea0bbd0499da to your computer and use it in GitHub Desktop.
965. Univalued Binary Tree : https://leetcode.com/problems/univalued-binary-tree/
This file contains 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
/** | |
* Definition for a binary tree node. | |
* struct TreeNode { | |
* int val; | |
* TreeNode *left; | |
* TreeNode *right; | |
* TreeNode(int x) : val(x), left(NULL), right(NULL) {} | |
* }; | |
*/ | |
class Solution { | |
public: | |
bool isSameValue(TreeNode* root, int val){ | |
if(root == NULL) return true; | |
if(root->val != val) return false; | |
bool ans = isSameValue(root->left, val); | |
ans &= isSameValue(root->right, val); | |
return ans; | |
} | |
bool isUnivalTree(TreeNode* root) { | |
return isSameValue(root, root->val); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment