You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
The text was updated successfully, but these errors were encountered:
class Solution:
def convertBST(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
if not root:
return None
cur = root
pre = 0
stack = []
# stack负责回溯,cur负责遍历 只有既没有回溯节点和遍历节点的时候,循环才会停止
while stack or cur:
if cur:
stack.append(cur)
cur = cur.right
else:
# 遇到空节点 回溯
cur = stack.pop()
cur.val += pre
pre = cur.val
cur = cur.left
return root
其他语言版本中,python迭代版本2中,result完全没有必要
The text was updated successfully, but these errors were encountered: