题解 | #二叉搜索树与双向链表#

二叉搜索树与双向链表

http://www.nowcoder.com/practice/947f6eb80d944a84850b0538bf0ec3a5

先用中序遍历 然后是遍历是把 节点加入到列表中 而不是加入节点的值 。然后生成双链表。

    def Convert(self , pRootOfTree ):
        if pRootOfTree is None:return None
        def proorder(root):
            res,stack =[],[]
            while stack or root:
                while root:
                    stack.append(root)
                    root=root.left
                node=stack.pop()
                res.append(node)
                root=node.right
            return res
        res=proorder(pRootOfTree)
        if len(res)==1:
            return res[0]
        for i in range(len(res)-1):
            res[i].right=res[i+1]
            res[i+1].left=res[i]
        return res[0]
    

        
        
全部评论

相关推荐

MScoding:你这个实习有一个是当辅导老师,这个和找技术岗没有关系吧?
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务