美文网首页
leetcode 897. 递增顺序查找树

leetcode 897. 递增顺序查找树

作者: fanchuang | 来源:发表于2020-03-06 20:25 被阅读0次

见注释

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