-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIsValidBST.java
More file actions
113 lines (102 loc) · 3.37 KB
/
Copy pathIsValidBST.java
File metadata and controls
113 lines (102 loc) · 3.37 KB
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
package LeetCode;
/**
* @FileName: IsValidBST.java
* @Description: 验证二叉查找树
* @Author: ABCpril
* @Date: 2021/12/07
*/
public class IsValidBST {
// ResultType法
public boolean isValidBST1(TreeNode root) {
return helper(root).isBST;
}
// 1.递归的定义:返回以root为根的子树的BST情况:是否是BST、左子树最大值、右子树最小值
private ResultType helper(TreeNode root) {
// 3.递归的出口
if (root == null) {
return new ResultType(true);
}
// 2.递归的拆解
ResultType leftRes = helper(root.left);
ResultType rightRes = helper(root.right);
// 左右子树结果对最终结果的作用
// 左子树或右子树不是BST
if (!leftRes.isBST || !rightRes.isBST) {
return new ResultType(false);
}
// 左子树不为空,BST要求root.val 严格> 左子树最大值
if (leftRes.maxValue != null && root.val <= leftRes.maxValue) {
return new ResultType(false);
}
// 右子树不为空,BST要求root.val 严格< 右子树最小值
if (rightRes.minValue != null && root.val >= rightRes.minValue) {
return new ResultType(false);
}
return new ResultType(true, leftRes.minValue == null ? root.val : leftRes.minValue,
rightRes.maxValue == null ? root.val : rightRes.maxValue);
}
class ResultType {
public boolean isBST;
public Integer minValue, maxValue;
public ResultType(boolean isBST, int min, int max) {
this.isBST = isBST;
minValue = min; maxValue = max;
}
public ResultType(boolean isBST) {
this.isBST = isBST;
minValue = null; maxValue = null;
}
}
// 中序遍历递归法
public boolean isValidBST2(TreeNode root) {
inOrderTraverse(root);
return isOrdered;
}
boolean isOrdered = true;
// -2^31 <= Node.val <= 2^31-1,正好是Integer的MIN_VALUE和MAX_VALUE
Integer pre = null;
// 1.递归的含义:以root为根的子树,左中右严格递增判别
private void inOrderTraverse(TreeNode root) {
// 3.递归的出口
if (root == null) return;
// 2.递归的拆解
inOrderTraverse(root.left);
if (pre != null && root.val <= pre) {
isOrdered = false;
return;
}
pre = root.val;
inOrderTraverse(root.right);
}
// 中序遍历非递归法
// -2^31 <= Node.val <= 2^31-1,正好是Integer的MIN_VALUE和MAX_VALUE
Integer preVal = null;
public boolean isValidBST3(TreeNode root) {
TreeNode[] stack = new TreeNode[10010];
int tt = 0;
TreeNode curt = root;
while (curt != null || tt > 0) {
while (curt != null) {
stack[++tt] = curt;
curt = curt.left;
}
curt = stack[tt];
tt--;
// res.add(curt.val);
if (preVal != null && curt.val <= preVal) {
return false;
}
preVal = curt.val;
curt = curt.right;
}
return true;
}
}
class TreeNode {
public int val;
public TreeNode left, right;
public TreeNode(int val) {
this.val = val;
this.left = this.right = null;
}
}