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
commit 3b8eff71cd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 20 additions and 0 deletions

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