Skip to content

Commit

Permalink
面试题 04.05. 合法二叉搜索树
Browse files Browse the repository at this point in the history
  • Loading branch information
zdRan committed Jan 25, 2021
1 parent fb1356b commit 7dfe5ef
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
|剑指 Offer 26|[树的子结构](#)|[Java](./src/com/leetcode/submissions/IsSubStructure.java)|Medium|
|剑指 Offer 55 - II|[平衡二叉树](#)|[Java](./src/com/leetcode/submissions/IsBalanced.java)|Easy|
|814 |[二叉树剪枝](#)|[Java](./src/com/leetcode/submissions/BinaryTreePruning.java)|Medium|
|面试题 04.05|[合法二叉搜索树](#)|[Java](./src/com/leetcode/submissions/IsValidBST.java)|Medium|


## 题目目录
Expand Down
28 changes: 28 additions & 0 deletions src/com/leetcode/submissions/IsValidBST.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.leetcode.submissions;

import com.leetcode.extend.TreeNode;

/**
* 面试题 04.05. 合法二叉搜索树
* Create by Ranzd on 2021-01-25 20:18
*
* @author [email protected]
*/
public class IsValidBST {
public long MAX_VAL = Long.MIN_VALUE;

public boolean isValidBST(TreeNode root) {
if (root == null) {
return true;
}
boolean left = isValidBST(root.left);
if (!left) {
return false;
}
if (MAX_VAL >= root.val) {
return false;
}
MAX_VAL = root.val;
return isValidBST(root.right);
}
}

0 comments on commit 7dfe5ef

Please sign in to comment.