Update 0538.把二叉搜索树转换为累加树.md

增加 python 迭代法
This commit is contained in:
ZerenZhang2022
2023-03-31 21:58:27 -04:00
committed by GitHub
parent f0db0605f3
commit 6943358d8f

View File

@ -234,6 +234,26 @@ class Solution:
return root
```
**迭代**
```python
class Solution:
def convertBST(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
if not root: return root
stack = []
result = []
cur = root
pre = 0
while cur or stack:
if cur:
stack.append(cur)
cur = cur.right
else:
cur = stack.pop()
cur.val+= pre
pre = cur.val
cur =cur.left
return root
```
## Go