题解 | #二叉树中和为某一值的路径(二)#
二叉搜索树与双向链表
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
查看21道真题和解析