-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem_2331_evaluateTree.cc
60 lines (52 loc) · 1.16 KB
/
Problem_2331_evaluateTree.cc
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
#include <iostream>
#include <vector>
#include "UnitTest.h"
using namespace std;
struct TreeNode
{
int val;
TreeNode *left;
TreeNode *right;
TreeNode() : val(0), left(nullptr), right(nullptr) {}
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};
class Solution
{
public:
bool dfs(TreeNode *node)
{
switch (node->val)
{
case 0:
return false;
case 1:
return true;
case 2:
return dfs(node->left) | dfs(node->right);
case 3:
return dfs(node->left) & dfs(node->right);
default:
return true;
}
}
bool evaluateTree(TreeNode *root) { return dfs(root); }
};
void testEvaluateTree()
{
Solution s;
TreeNode *x5 = new TreeNode(1, nullptr, nullptr);
TreeNode *x4 = new TreeNode(0, nullptr, nullptr);
TreeNode *x3 = new TreeNode(3, x4, x5);
TreeNode *x2 = new TreeNode(1, nullptr, nullptr);
TreeNode *x1 = new TreeNode(2, x2, x3);
TreeNode *y = new TreeNode(0);
EXPECT_TRUE(s.evaluateTree(x1));
EXPECT_FALSE(s.evaluateTree(y));
EXPECT_SUMMARY;
}
int main()
{
testEvaluateTree();
return 0;
}