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 ba9c878

Browse files
🐳(dfs): 98. 验证二叉搜索树
补充 while 中序遍历
1 parent 819be30 commit ba9c878

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

‎docs/data-structure/tree/dfs/README.md‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,35 @@ func helper(root *TreeNode, low int, high int) bool {
231231
}
232232
```
233233

234+
### 解三:栈辅助中序遍历
235+
236+
```python
237+
# Definition for a binary tree node.
238+
# class TreeNode:
239+
# def __init__(self, val=0, left=None, right=None):
240+
# self.val = val
241+
# self.left = left
242+
# self.right = right
243+
class Solution:
244+
def isValidBST(self, root: TreeNode) -> bool:
245+
stack = []
246+
pre = float('-inf')
247+
while root is not None or len(stack) > 0:
248+
# 左侧压入栈
249+
while root is not None:
250+
stack.append(root)
251+
root = root.left
252+
# 弹出栈顶
253+
top = stack.pop()
254+
# 判断终序遍历前 1 个数是否小于当前数
255+
if top.val <= pre:
256+
return False
257+
pre = top.val
258+
root = top.right
259+
260+
return True
261+
```
262+
234263
<!-- tabs:end -->
235264

236265
## 105. 从前序与中序遍历序列构造二叉树

0 commit comments

Comments
(0)

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