-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0098.cpp
92 lines (83 loc) · 1.6 KB
/
0098.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
#include <iostream>
#include <vector>
#include <stack>
using namespace std;
struct TreeNode
{
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) :val(x), left(NULL), right(NULL) {}
};
class Solution
{
public:
/*
// 中序遍历
// 中序遍历下,输出的二叉搜索树节点的数值是有序序列
// 1. 确定参数和返回参数
TreeNode* pre = NULL;
bool isValidBST(TreeNode* root)
{
// 2. 确定终止条件
if (root == NULL)
return true;
// 3. 单次递归处理
bool left = isValidBST(root->left); // 左
if (pre != NULL && pre->val >= root->val) // 中
return false;
pre = root;
bool right = isValidBST(root->right); // 右
return left && right;
}
*/
// 中序遍历 迭代法
bool isValidBST(TreeNode* root)
{
if (root == NULL)
return true;
stack<TreeNode*> st;
st.push(root);
TreeNode* pre = NULL;
while (!st.empty())
{
TreeNode* node = st.top();
// 访问
if (node != NULL)
{
st.pop();
if (node->right != NULL) // 右
st.push(node->right);
st.push(node); // 中
st.push(NULL);
if (node->left != NULL) // 左
st.push(node->left);
}
// 处理
else
{
st.pop();
node = st.top();
st.pop();
if (pre != NULL && pre->val >= node->val)
return false;
pre = node;
}
}
return true;
}
};
int main()
{
TreeNode* root = new TreeNode(4);
TreeNode* node = root;
node->left = new TreeNode(2);
node->right = new TreeNode(7);
node->left->left = new TreeNode(1);
node->left->right = new TreeNode(3);
// node->right->left = new TreeNode(4);
// node->right->right = new TreeNode(7);
Solution S;
S.isValidBST(root);
return 0;
}