parent
f0db0605f3
commit
6943358d8f
|
|
@ -234,6 +234,26 @@ class Solution:
|
||||||
return root
|
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
|
## Go
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue