题解 | #二叉搜索树与双向链表#
二叉搜索树与双向链表
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 Convert(self , pRootOfTree ):
# write code here
def trans(p):
if not p.left and not p.right:
return p, p
ll, lr, rl, rr = None, None, None, None
if p.left:
ll, lr = trans(p.left)
if p.right:
rl, rr = trans(p.right)
if lr:
p.left = lr
lr.right = p
if rl:
p.right = rl
rl.left = p
if not rr:
rr = p
if not ll:
ll = p
return ll, rr
if not pRootOfTree:
return None
ll, rr = trans(pRootOfTree)
return ll
#牛客专项练习#
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
#
#
# @param pRootOfTree TreeNode类
# @return TreeNode类
#
class Solution:
def Convert(self , pRootOfTree ):
# write code here
def trans(p):
if not p.left and not p.right:
return p, p
ll, lr, rl, rr = None, None, None, None
if p.left:
ll, lr = trans(p.left)
if p.right:
rl, rr = trans(p.right)
if lr:
p.left = lr
lr.right = p
if rl:
p.right = rl
rl.left = p
if not rr:
rr = p
if not ll:
ll = p
return ll, rr
if not pRootOfTree:
return None
ll, rr = trans(pRootOfTree)
return ll
#牛客专项练习#