-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheveluate_bollean_binary_tree.cpp
44 lines (35 loc) · 1.11 KB
/
eveluate_bollean_binary_tree.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
class Solution {
public:
bool evaluateTree(TreeNode* root) {
if(root->val==2)
return evaluateTree(root->left) || evaluateTree(root->right) ;
if(root->val==3)
return evaluateTree(root->left) && evaluateTree(root->right);
else
return root->val;
}
};
/*
eveluate bollean binary tree
class Solution {
public:
bool evaluateTree(TreeNode* root) {
if( root->val == 1)return true;
if( root->val ==0)return false;
if (root->val ==2)return evaluateTree(root->left) or evaluateTree(root->right);
if(root->val ==3)return evaluateTree(root->left) and evaluateTree (root->right);
return true;
}
};
methods 2
class Solution {
public:
bool evaluateTree(TreeNode* root) {
// base case: leaf
if (root->val < 2) return root->val;
// or node
else if (root->val == 2) return evaluateTree(root->left) || evaluateTree(root->right);
// and node
return evaluateTree(root->left) && evaluateTree(root->right);
}
};