mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
Update 0098.验证二叉搜索树.md
新增java 統一迭代法的寫法 有通過. AC
This commit is contained in:
@ -259,6 +259,36 @@ public:
|
||||
|
||||
## Java
|
||||
|
||||
```Java
|
||||
//使用統一迭代法
|
||||
class Solution {
|
||||
public boolean isValidBST(TreeNode root) {
|
||||
Stack<TreeNode> stack = new Stack<>();
|
||||
TreeNode pre = null;
|
||||
if(root != null)
|
||||
stack.add(root);
|
||||
while(!stack.isEmpty()){
|
||||
TreeNode curr = stack.peek();
|
||||
if(curr != null){
|
||||
stack.pop();
|
||||
if(curr.right != null)
|
||||
stack.add(curr.right);
|
||||
stack.add(curr);
|
||||
stack.add(null);
|
||||
if(curr.left != null)
|
||||
stack.add(curr.left);
|
||||
}else{
|
||||
stack.pop();
|
||||
TreeNode temp = stack.pop();
|
||||
if(pre != null && pre.val >= temp.val)
|
||||
return false;
|
||||
pre = temp;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
```
|
||||
```Java
|
||||
class Solution {
|
||||
// 递归
|
||||
|
Reference in New Issue
Block a user