mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
Merge pull request #2000 from ZerenZhang2022/patch-20
Update 0538.把二叉搜索树转换为累加树.md
This commit is contained in:
@ -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
|
||||
|
||||
|
Reference in New Issue
Block a user