-
Notifications
You must be signed in to change notification settings - Fork 0
/
code_98.cpp
185 lines (148 loc) · 4.02 KB
/
code_98.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include "code.h"
#include <vector>
#include "gtest/gtest.h"
#include <map>
#include <utility>
#include <limits.h>
using namespace std;
#define DEBUG
#ifdef DEBUG
#define OJDBG std::cout
#else
#define OJDBG 0 && std::cout
#endif
void call_code_98(){}
/**
* 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_98 {
private:
TreeNode* getLeftLeaf()
{
TreeNode *node = m_root;
while(node->left != nullptr)
{
node = node->left;
}
return node;
}
TreeNode* getRightLeaf()
{
TreeNode* node = m_root;
while(node->right != nullptr)
{
node = node->right;
}
return node;
}
TreeNode* m_root;
public:
bool isValidBST(TreeNode* root) {
// 空为合法
if (root == nullptr)
return true;
// 单独一个根节点合法
if (root->left == nullptr && root->right == nullptr)
return true;
m_root = root;
// 先求出最左叶子结点作为lower,以及最右叶子结点作为upper
int lower = getLeftLeaf()->val;
int upper = getRightLeaf()->val;
return isValidBST(root, lower, upper);
}
bool isValidBST(TreeNode* root, int lower, int upper)
{
OJDBG << "lower=" << lower << " upper=" << upper << endl;
if (root->val == lower)
{
// 等于下界时,只能是最左叶子,否则都是错误
if (root == getLeftLeaf() && root != m_root)
return true;
else
return false;
}
if (root->val == upper)
{
// 等于上界时,只能是最右叶子,否则都是错误
if (root == getRightLeaf() && root != m_root)
return true;
else
return false;
}
if (root->val < lower || root->val > upper)
{
// 超过上下界就是错误
OJDBG << "Out of bound! val=" << root->val << endl;
return false;
}
root->Print();
// 1. 从根节点出发,有左子树就看左子树,递归进入
// 2. 没有左子树就看右子树,递归进入
// 3. 递归进入时,判断子树是否满足要求,若不满足直接返回失败
// 4. 向左进入,则缩小upper,向右进入,则放大lower
if (root->left != nullptr)
{
int newUpper = root->val;
OJDBG << "newUpper=" << newUpper << endl;
if(!isValidBST(root->left, lower, newUpper))
{
// 迭代退出,一路返回失败
return false;
}
}
if (root->right != nullptr)
{
int newLower = root->val;
OJDBG << "newLower=" << newLower << endl;
if(!isValidBST(root->right, newLower, upper))
{
return false;
}
}
return true;
}
};
TEST(Test_Code_98, test1)
{
TreeNode root(2);
TreeNode left(1);
TreeNode right(3);
root.left = &left;
root.right = &right;
Solution_98 sln;
EXPECT_EQ(sln.isValidBST(&root), true);
}
TEST(Test_Code_98, test2)
{
TreeNode root(5);
TreeNode node_51(1);
TreeNode node_54(4);
TreeNode node_43(3);
TreeNode node_46(6);
root.left = &node_51;
root.right = &node_54;
node_54.left = &node_43;
node_54.right = &node_46;
Solution_98 sln;
EXPECT_EQ(sln.isValidBST(&root), false);
}
TEST(Test_Code_98, test3)
{
TreeNode root(10);
TreeNode node_10_5(5);
TreeNode node_10_15(15);
TreeNode node_15_6(6);
TreeNode node_15_20(20);
root.left = &node_10_5;
root.right = &node_10_15;
node_10_15.left = &node_15_6;
node_10_15.right = &node_15_20;
Solution_98 sln;
EXPECT_EQ(sln.isValidBST(&root), false);
}