897. Increasing Order Search Tree
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def increasingBST(self, root):
"""
:type root: TreeNode
:rtype: TreeNode
"""
#if root is None: return root
def helper(node):
if node is None: return None, None
if node.left is None and node.right is None:
return node, node
left_root, left_end = None, None
right_root, right_end = None, None
if node.left:
left_root, left_end = helper(node.left)
if node.right:
right_root, right_end = helper(node.right)
first, end = None, None
if left_root:
first = left_root
# left_end = node
left_end.right = node
else:
first = node
node.left = None
if right_root:
end = right_end
node.right = right_root
else:
end = node
return first, end
return helper(root)[0]
在连接左边子树和当前结点的时候出错了。
# left_end = node
left_end.right = node