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

二叉搜索树与双向链表

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

# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

#
# 
# @param pRootOfTree TreeNode类 
# @return TreeNode类
#
class Solution:
    def PreNode(self, root : TreeNode):
        this = root
        if not this:
            return None
        if this.left:
            this = this.left
            while this.right:
                this = this.right
            root.left = this
            this.right = root
    def RearNode(self, root : TreeNode):
        this = root
        if not this:
            return None
        if this.right:
            this = this.right
            while this.left:
                this = this.left
            root.right = this
            this.left = root
    def LDR(self, this : TreeNode):
        if this:
            self.LDR(this.left)
            # 遍历完左孩子后,左指针解放,于是可以将左指针指向前驱
            self.PreNode(this)

            self.LDR(this.right)
            # 遍历完右孩子后,右指针解放,于是可以将右指针指向后继
            self.RearNode(this)

    def Convert(self , pRootOfTree ):
        # write code here
        this = pRootOfTree
        if not this:
            return None
        # 找到最小节点,作为头节点
        while this.left:
            this = this.left
        head = this
        self.LDR(pRootOfTree)
        return head

全部评论

相关推荐

努力的小明a:项目看着很眼熟,施磊老师吧,我也学的这个😋我当时是把rpc框架做成了一个分布式网盘,这是一个项目,然后muduo库做成集群即时通讯,又用QT做了个交互的客户端,这样又一个项目,然后一个轻量redis,一个CAD,总共四个项目,投了三个月就今天2月份一个小厂Qt offer,然后后面想开了,Qt啥的都能干,这个月get了个北京大厂的offer,做java后端,人生就是这么魔幻,现在就在去北京入职的路上
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务