Merge pull request #2055 from Lozakaka/patch-5

Update 0098.验证二叉搜索树.md
This commit is contained in:
程序员Carl
2023-05-14 10:23:47 +08:00
committed by GitHub

View File

@ -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 {
// 递归