Merge pull request #2000 from ZerenZhang2022/patch-20

Update 0538.把二叉搜索树转换为累加树.md
This commit is contained in:
程序员Carl
2023-04-13 10:23:45 +08:00
committed by GitHub

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