-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode98.java
More file actions
66 lines (60 loc) · 2.26 KB
/
LeetCode98.java
File metadata and controls
66 lines (60 loc) · 2.26 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
import util.TreeNode;
public class LeetCode98 {
public static void main(String[] args) {
// 输入:root = [2,1,3]
// 输出:true
System.out.println(new Solution98_1().isValidBST(TreeNode.buildTree(new Integer[] { 2, 1, 3 })));
// 输入:root = [5,1,4,null,null,3,6]
// 输出:false
System.out
.println(
new Solution98_1().isValidBST(TreeNode.buildTree(new Integer[] { 5, 1, 4, null, null, 3, 6 })));
// 输入:[5,4,6,null,null,3,7]
// 输出:true
System.out
.println(
new Solution98_1().isValidBST(TreeNode.buildTree(new Integer[] { 5, 4, 6, null, null, 3, 7 })));
}
}
class Solution98_1 {
// NOTE: 假定左右子树均为二叉查找树,那么判断左/右子树的最大/最小节点是否满足要求
public boolean isValidBST(TreeNode root) {
if (root == null) {
return true;
}
if (isValidBST(root.left) && isValidBST(root.right)) {
TreeNode leftSubTreeMax = root.left;
while (leftSubTreeMax != null && leftSubTreeMax.right != null) {
leftSubTreeMax = leftSubTreeMax.right;
}
if (leftSubTreeMax != null && leftSubTreeMax.val >= root.val) {
return false;
}
TreeNode rightSubTreeMin = root.right;
while (rightSubTreeMin != null && rightSubTreeMin.left != null) {
rightSubTreeMin = rightSubTreeMin.left;
}
if (rightSubTreeMin != null && rightSubTreeMin.val <= root.val) {
return false;
}
return true;
} else {
return false;
}
}
}
class Solution98_2 {
// NOTE: 用上下界限来判断root的值是否合理
public boolean isValidBST(TreeNode root) {
return isValidBST(root, Long.MIN_VALUE, Long.MAX_VALUE);
}
public boolean isValidBST(TreeNode node, long lower, long upper) {
if (node == null) {
return true;
}
if (node.val <= lower || node.val >= upper) {
return false;
}
return isValidBST(node.left, lower, node.val) && isValidBST(node.right, node.val, upper);
}
}