-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path98.ValidateBinarySearchTree.java
More file actions
53 lines (46 loc) · 1.69 KB
/
98.ValidateBinarySearchTree.java
File metadata and controls
53 lines (46 loc) · 1.69 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
// 自己的思路:用了额外的空间
// 执行用时:1 ms, 在所有 Java 提交中击败了32.29%的用户
// 内存消耗:38.1 MB, 在所有 Java 提交中击败了56.49%的用户
class Solution {
public boolean isValidBST(TreeNode root) {
inorderTraversal(root);
return result;
}
List<Integer> res = new ArrayList();
Boolean result = true;
void inorderTraversal(TreeNode root) {
if (root == null || !result)
return;
inorderTraversal(root.left);
int length = res.size();
if (length > 0) {
int lastElement = res.get(res.size() - 1);
if (lastElement >= root.val) {
result = false;
}
}
res.add(root.val);
inorderTraversal(root.right);
}
}
// 别人的思路
// 执行用时:0 ms, 在所有 Java 提交中击败了100%的用户
// 内存消耗:37.4 MB, 在所有 Java 提交中击败了99.94%的用户
class Solution {
boolean isValidBST(TreeNode root) {
return isValidBST(root, null, null);
}
/* 限定以 root 为根的子树节点必须满足 max.val > root.val > min.val */
boolean isValidBST(TreeNode root, TreeNode min, TreeNode max) {
// base case
if (root == null)
return true;
// 若 root.val 不符合 max 和 min 的限制,说明不是合法 BST
if (min != null && root.val <= min.val)
return false;
if (max != null && root.val >= max.val)
return false;
// 限定左子树的最大值是 root.val,右子树的最小值是 root.val
return isValidBST(root.left, min, root) && isValidBST(root.right, root, max);
}
}