题解 | #二叉树中和为某一值的路径(二)#

二叉搜索树与双向链表

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

''' 采用递归 第一:中序遍历

先左子树:将指针移到最右点p 连接p与根节点 再连接根节点与右子树

'''

-- coding:utf-8 --

class TreeNode:

def init(self, x):

self.val = x

self.left = None

self.right = None

class Solution: def Convert(self, pRootOfTree): # write code here if pRootOfTree is None: return None

    left=self.Convert(pRootOfTree.left)
    p=left
    
    while left and p.right:
        p=p.right
    
    #链接左子树与根节点
    if left:
        p.right=pRootOfTree
        pRootOfTree.left=p
    
    right=self.Convert(pRootOfTree.right)
    q=right
    if right:
        pRootOfTree.right=q
        q.left=pRootOfTree
    if left:
        return left
    else:
        return pRootOfTree 
    
    
    
    
    
全部评论

相关推荐

点赞 评论 收藏
分享
05-15 14:58
已编辑
南昌航空大学科技学院 C++
mcart:上海150怎么活,睡公司吗
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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