parent
b253fccfba
commit
bb8e7312b6
|
|
@ -259,6 +259,36 @@ public:
|
||||||
|
|
||||||
## Java
|
## 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
|
```Java
|
||||||
class Solution {
|
class Solution {
|
||||||
// 递归
|
// 递归
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue