-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path965.cpp
35 lines (34 loc) · 878 Bytes
/
965.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/**
* 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 isUnivalTree(TreeNode* root) {
stack<TreeNode*> nodestk;
int univalue=root->val;
nodestk.push(root);
if(root==NULL)
return true;
while(!nodestk.empty()){
root=nodestk.top();
nodestk.pop();
if(root->left!=NULL){
if(root->left->val!=univalue)
return false;
nodestk.push(root->left);
}
if(root->right!=NULL){
if(root->right->val!=univalue)
return false;
nodestk.push(root->right);
}
}
return true;
}
};