leetcode 897. 递增顺序查找树
见注释
class Solution:
def increasingBST(self, root: TreeNode) -> TreeNode:
# 官方题解2, 需要再看看
# 重新排列树,是否需要新建一棵树呢
# None 和 TreeNode(None) 是有区别的。。。
stack = []
vals = []
while root or stack:
while root:
stack.append(root)
root = root.left
root = stack.pop()
# print(root.val)
vals.append(root.val)
root = root.right
# print(vals)
t = TreeNode(vals.pop(0))
s = t
while vals:
s.left = None
s.right = TreeNode(vals.pop(0))
s = s.right
return t
本文标题:leetcode 897. 递增顺序查找树
本文链接:https://www.haomeiwen.com/subject/nrvnrhtx.html
网友评论