Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # 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 flatten(self, root):
- """
- :type root: TreeNode
- :rtype: void Do not return anything, modify root in-place instead.
- """
- if not root:
- return
- #make sure cope with left tree and right tree before insert
- #left tree into middle of root and right tree
- self.flatten(root.left)
- self.flatten(root.right)
- if root.left == None: # it means we do not need to do anything
- return
- p = root.left # go to the bottom of root's left tree
- while p.right:
- p = p.right
- p.right = root.right
- root.right = root.left
- root.left = None
Add Comment
Please, Sign In to add comment