Skip to content

Commit 4d7468b

Browse files
committed
三刷98
1 parent 1899bee commit 4d7468b

File tree

11 files changed

+121
-45
lines changed

11 files changed

+121
-45
lines changed
Lines changed: 52 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,64 @@
11
[#0098-validate-binary-search-tree]
2-
= 98. Validate Binary Search Tree
2+
= 98. 验证二叉搜索树
33

4-
{leetcode}/problems/validate-binary-search-tree/[LeetCode - Validate Binary Search Tree^]
4+
https://leetcode.cn/problems/validate-binary-search-tree/[LeetCode - 98. 验证二叉搜索树^]
55

6-
Given a binary tree, determine if it is a valid binary search tree (BST).
6+
给你一个二叉树的根节点 `root` ,判断其是否是一个有效的二叉搜索树。
77

8-
Assume a BST is defined as follows:
8+
*有效* 二叉搜索树定义如下:
99

10+
* 节点的左子树只包含 *小于* 当前节点的数。
11+
* 节点的右子树只包含 *大于* 当前节点的数。
12+
* 所有左子树和右子树自身必须也是二叉搜索树。
1013
11-
* The left subtree of a node contains only nodes with keys *less than* the node's key.
12-
* The right subtree of a node contains only nodes with keys *greater than* the node's key.
13-
* Both the left and right subtrees must also be binary search trees.
14+
*示例 1:*
1415

16+
image::images/0098-01.jpg[{image_attr}]
1517

16-
18+
....
19+
输入:root = [2,1,3]
20+
输出:true
21+
....
1722

18-
*Example 1:*
23+
*示例 2:*
1924

20-
[subs="verbatim,quotes,macros"]
21-
----
22-
2
23-
/ \
24-
1 3
25+
image::images/0098-02.jpg[{image_attr}]
2526

26-
*Input:* [2,1,3]
27-
*Output:* true
28-
----
27+
....
28+
输入:root = [5,1,4,null,null,3,6]
29+
输出:false
30+
解释:根节点的值是 5 ,但是右子节点的值是 4 。
31+
....
2932

30-
*Example 2:*
33+
*提示:*
34+
35+
* 树中节点数目范围在`[1, 10^4^]`
36+
* `-2^31^ \<= Node.val \<= 2^31^ - 1`
3137
32-
[subs="verbatim,quotes,macros"]
33-
----
34-
5
35-
/ \
36-
1 4
37-
/ \
38-
3 6
39-
40-
*Input:* [5,1,4,null,null,3,6]
41-
*Output:* false
42-
*Explanation:* The root node's value is 5 but its right child's value is 4.
43-
----
4438
4539
== 思路分析
4640

47-
image::images/0098-1.png[{image_attr}]
41+
image::images/0098-14.png[{image_attr}]
4842

49-
image::images/0098-2.png[{image_attr}]
43+
image::images/0098-15.png[{image_attr}]
5044

5145
思路很简单,利用搜索二叉树的定义,界定好树的上下界,然后递归比较就好。
5246

47+
48+
直接使用“树形DP套路”+剪枝技巧,速度直接击败 100%。
49+
50+
这里有一点需要注意:最大值最小值用 `Long.MIN_VALUE` 和 `Long.MAX_VALUE`,这样可以防止单节点树 `Integer.MAX_VALUE` (最小值的单节点树应该也会有问题)造成的错误。
51+
52+
另外,查看了官方题解后,发现可以使用树的中序排列来检查(二叉搜索树中序排列是升序),这样跟前几天在牛客网上做的那个“发现二叉搜索树中的两个错误节点”的思路就一致了。回头尝试一下。
53+
54+
image::images/0098-10.png[{image_attr}]
55+
56+
image::images/0098-11.png[{image_attr}]
57+
58+
image::images/0098-12.png[{image_attr}]
59+
60+
image::images/0098-13.png[{image_attr}]
61+
5362
[[src-0098]]
5463
[tabs]
5564
====
@@ -70,22 +79,20 @@ include::{sourcedir}/_0098_ValidateBinarySearchTree.java[tag=answer]
7079
include::{sourcedir}/_0098_ValidateBinarySearchTree_2.java[tag=answer]
7180
----
7281
--
73-
====
7482
75-
直接使用“树形DP套路”+剪枝技巧,速度直接击败 100%。
76-
77-
这里有一点需要注意:最大值最小值用 `Long.MIN_VALUE` 和 `Long.MAX_VALUE`,这样可以防止单节点树 `Integer.MAX_VALUE` (最小值的单节点树应该也会有问题)造成的错误。
78-
79-
另外,查看了官方题解后,发现可以使用树的中序排列来检查(二叉搜索树中序排列是升序),这样跟前几天在牛客网上做的那个“发现二叉搜索树中的两个错误节点”的思路就一致了。回头尝试一下。
80-
81-
image::images/0098-01.png[{image_attr}]
82-
83-
image::images/0098-02.png[{image_attr}]
84-
85-
image::images/0098-03.png[{image_attr}]
83+
二刷::
84+
+
85+
--
86+
[{java_src_attr}]
87+
----
88+
include::{sourcedir}/_0098_ValidateBinarySearchTree_3.java[tag=answer]
89+
----
90+
--
91+
====
8692

87-
image::images/0098-04.png[{image_attr}]
8893

8994
== 参考资料
9095

96+
. https://leetcode.cn/problems/validate-binary-search-tree/solutions/2020306/qian-xu-zhong-xu-hou-xu-san-chong-fang-f-yxvh/[98. 验证二叉搜索树 - 前序中序后序,三种方法,一个视频讲透!^]
9197
. https://leetcode.cn/problems/validate-binary-search-tree/solutions/230256/yan-zheng-er-cha-sou-suo-shu-by-leetcode-solution/[98. 验证二叉搜索树 - 官方题解^]
98+
. https://leetcode.cn/problems/validate-binary-search-tree/solutions/231280/yi-zhang-tu-rang-ni-ming-bai-shang-xia-jie-zui-da-/[98. 验证二叉搜索树 - 一张图让你明白上下界(最大值最小值)判定^]

docs/images/0098-01.jpg

6.67 KB
Loading

docs/images/0098-02.jpg

12 KB
Loading
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

logbook/202503.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1899,6 +1899,11 @@ endif::[]
18991899
|{doc_base_url}/0104-maximum-depth-of-binary-tree.adoc[题解]
19001900
|✅ 深度优先遍历。
19011901

1902+
|{counter:codes2503}
1903+
|{leetcode_base_url}/validate-binary-search-tree/[98. 验证二叉搜索树^]
1904+
|{doc_base_url}/0098-validate-binary-search-tree.adoc[题解]
1905+
|✅ 树形DP套路或中序遍历
1906+
19021907
|===
19031908

19041909
截止目前,本轮练习一共完成 {codes2503} 道题。

0 commit comments

Comments
 (0)