Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit bb8e731

Browse files
Update 0098.验证二叉搜索树.md
新增java 統一迭代法的寫法 有通過. AC
1 parent b253fcc commit bb8e731

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

‎problems/0098.验证二叉搜索树.md‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,36 @@ public:
259259

260260
## Java
261261

262+
```Java
263+
//使用統一迭代法
264+
class Solution {
265+
public boolean isValidBST(TreeNode root) {
266+
Stack<TreeNode> stack = new Stack<>();
267+
TreeNode pre = null;
268+
if(root != null)
269+
stack.add(root);
270+
while(!stack.isEmpty()){
271+
TreeNode curr = stack.peek();
272+
if(curr != null){
273+
stack.pop();
274+
if(curr.right != null)
275+
stack.add(curr.right);
276+
stack.add(curr);
277+
stack.add(null);
278+
if(curr.left != null)
279+
stack.add(curr.left);
280+
}else{
281+
stack.pop();
282+
TreeNode temp = stack.pop();
283+
if(pre != null && pre.val >= temp.val)
284+
return false;
285+
pre = temp;
286+
}
287+
}
288+
return true;
289+
}
290+
}
291+
```
262292
```Java
263293
class Solution {
264294
// 递归

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /