-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy path965.cpp
63 lines (56 loc) · 1.67 KB
/
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
__________________________________________________________________________________________________
sample 4 ms submission
/**
* 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,int value){
if(root==NULL) return true;
if(root->val != value) return false;
return isUnivalTree(root->left, value) && isUnivalTree(root->right, value);
}
bool isUnivalTree(TreeNode* root) {
if(root==NULL) return true;
int value=root->val;
return isUnivalTree(root, value);
}
};
__________________________________________________________________________________________________
sample 10568 kb submission
/**
* 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 recurse( TreeNode* root, int val )
{
if( !root ) return true;
if( root->val != val ) return false;
return recurse( root->left, val ) && recurse( root->right, val );
}
bool isUnivalTree(TreeNode* root) {
if( !root ) return true;
return recurse( root, root->val );
}
};
static int x = []() {
// toggle off cout & cin, instead, use printf & scanf
std::ios::sync_with_stdio(false);
// untie cin & cout
cin.tie(NULL);
return 0;
}();
__________________________________________________________________________________________________