Skip to content

Commit 7dfe5ef

Browse files
committed
面试题 04.05. 合法二叉搜索树
1 parent fb1356b commit 7dfe5ef

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
|剑指 Offer 26|[树的子结构](#)|[Java](./src/com/leetcode/submissions/IsSubStructure.java)|Medium|
2424
|剑指 Offer 55 - II|[平衡二叉树](#)|[Java](./src/com/leetcode/submissions/IsBalanced.java)|Easy|
2525
|814 |[二叉树剪枝](#)|[Java](./src/com/leetcode/submissions/BinaryTreePruning.java)|Medium|
26+
|面试题 04.05|[合法二叉搜索树](#)|[Java](./src/com/leetcode/submissions/IsValidBST.java)|Medium|
2627

2728

2829
## 题目目录
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.leetcode.submissions;
2+
3+
import com.leetcode.extend.TreeNode;
4+
5+
/**
6+
* 面试题 04.05. 合法二叉搜索树
7+
* Create by Ranzd on 2021-01-25 20:18
8+
*
9+
10+
*/
11+
public class IsValidBST {
12+
public long MAX_VAL = Long.MIN_VALUE;
13+
14+
public boolean isValidBST(TreeNode root) {
15+
if (root == null) {
16+
return true;
17+
}
18+
boolean left = isValidBST(root.left);
19+
if (!left) {
20+
return false;
21+
}
22+
if (MAX_VAL >= root.val) {
23+
return false;
24+
}
25+
MAX_VAL = root.val;
26+
return isValidBST(root.right);
27+
}
28+
}

0 commit comments

Comments
 (0)