Skip to content

Commit 7d55136

Browse files
committed
再次完成98
1 parent db1b8e5 commit 7d55136

File tree

8 files changed

+93
-1
lines changed

8 files changed

+93
-1
lines changed

README.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,7 @@
694694
|{source_base_url}/_0098_ValidateBinarySearchTree.java[Java]
695695
|{doc_base_url}/0098-validate-binary-search-tree.adoc[Note]
696696
|Medium
697-
|
697+
|树形DP套路或中序遍历
698698

699699
//|99
700700
//|{leetcode_base_url}/recover-binary-search-tree/[Recover Binary Search Tree]

docs/0000-21-dynamic-programming.adoc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ TIP: 写出信息结构是把所有的信息都装到一个对象中。如果只
3434
+
3535
. 第四步:设计递归函数,递归函数是处理以 X 为头节点的情况下的大难,包括设计递归的 base case,默认直接得到左树和右树的所有信息,以及把可能性做整合,并且要返回第三步的信息结构。
3636

37+
=== 相关试题
38+
39+
. xref:0098-validate-binary-search-tree.adoc[98. Validate Binary Search Tree]
40+
3741
== 参考资料
3842

3943
. 左程云《程序员代码面试指南》

docs/0098-validate-binary-search-tree.adoc

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
[#0098-validate-binary-search-tree]
12
= 98. Validate Binary Search Tree
23

34
https://leetcode.com/problems/validate-binary-search-tree/[LeetCode - Validate Binary Search Tree]
@@ -54,3 +55,26 @@ Assume a BST is defined as follows:
5455
include::{sourcedir}/_0098_ValidateBinarySearchTree.java[]
5556
----
5657

58+
59+
[{java_src_attr}]
60+
----
61+
include::{sourcedir}/_0098_ValidateBinarySearchTree_2.java[]
62+
----
63+
64+
直接使用“树形DP套路”+剪枝技巧,速度直接击败 100%。
65+
66+
这里有一点需要注意:最大值最小值用 `Long.MIN_VALUE``Long.MAX_VALUE`,这样可以防止单节点树 `Integer.MAX_VALUE` (最小值的单节点树应该也会有问题)造成的错误。
67+
68+
另外,查看了官方题解后,发现可以使用树的中序排列来检查(二叉搜索树中序排列是升序),这样跟前几天在牛客网上做的那个“发现二叉搜索树中的两个错误节点”的思路就一致了。回头尝试一下。
69+
70+
image::images/0098-01.png[]
71+
72+
image::images/0098-02.png[]
73+
74+
image::images/0098-03.png[]
75+
76+
image::images/0098-04.png[]
77+
78+
== 参考资料
79+
80+
. https://leetcode.cn/problems/validate-binary-search-tree/solutions/230256/yan-zheng-er-cha-sou-suo-shu-by-leetcode-solution/[98. 验证二叉搜索树 - 官方题解^]

docs/images/0098-01.png

78.4 KB
Loading

docs/images/0098-02.png

84.5 KB
Loading

docs/images/0098-03.png

79.1 KB
Loading

docs/images/0098-04.png

75.4 KB
Loading
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.diguage.algorithm.leetcode;
2+
3+
import com.diguage.algorithm.util.TreeNode;
4+
5+
import java.util.Arrays;
6+
7+
import static com.diguage.algorithm.util.TreeNodeUtils.buildTree;
8+
9+
public class _0098_ValidateBinarySearchTree_2 {
10+
public boolean isValidBST(TreeNode root) {
11+
return dfs(root).valid;
12+
}
13+
14+
private Record dfs(TreeNode node) {
15+
if (node == null) {
16+
return new Record(true, Long.MIN_VALUE, Long.MAX_VALUE);
17+
}
18+
Record left = dfs(node.left);
19+
if (!left.valid) {
20+
return new Record(false, Long.MIN_VALUE, Long.MAX_VALUE);
21+
}
22+
Record right = dfs(node.right);
23+
if (!right.valid) {
24+
return new Record(false, Long.MIN_VALUE, Long.MAX_VALUE);
25+
}
26+
27+
long max = Math.max(left.max, Math.max(node.val, right.max));
28+
long min = Math.min(left.min, Math.min(node.val, right.min));
29+
30+
return new Record(left.valid && right.valid
31+
&& left.max < node.val && node.val < right.min, max, min);
32+
}
33+
34+
public static class Record {
35+
public boolean valid;
36+
public long max;
37+
public long min;
38+
39+
public Record(boolean valid, long max, long min) {
40+
this.valid = valid;
41+
this.max = max;
42+
this.min = min;
43+
}
44+
}
45+
46+
public static void main(String[] args) {
47+
_0098_ValidateBinarySearchTree_2 solution = new _0098_ValidateBinarySearchTree_2();
48+
49+
boolean r5 = solution.isValidBST(buildTree(Arrays.asList(2147483647)));
50+
System.out.println(r5);
51+
52+
boolean r4 = solution.isValidBST(buildTree(Arrays.asList(3, 1, 5, 0, 2, 4, 6, null, null, null, 3)));
53+
System.out.println(r4);
54+
55+
boolean r3 = solution.isValidBST(buildTree(Arrays.asList(10, 5, 15, null, null, 6, 20)));
56+
System.out.println(r3);
57+
58+
boolean r1 = solution.isValidBST(buildTree(Arrays.asList(2, 1, 3)));
59+
System.out.println(r1);
60+
61+
boolean r2 = solution.isValidBST(buildTree(Arrays.asList(5, 1, 4, null, null, 3, 6)));
62+
System.out.println(r2);
63+
}
64+
}

0 commit comments

Comments
 (0)