Guest User

Untitled

a guest
Jul 19th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 KB | None | 0 0
  1. # Definition for a binary tree node.
  2. # class TreeNode(object):
  3. # def __init__(self, x):
  4. # self.val = x
  5. # self.left = None
  6. # self.right = None
  7.  
  8. class Solution(object):
  9. def flatten(self, root):
  10. """
  11. :type root: TreeNode
  12. :rtype: void Do not return anything, modify root in-place instead.
  13. """
  14. if not root:
  15. return
  16. #make sure cope with left tree and right tree before insert
  17. #left tree into middle of root and right tree
  18. self.flatten(root.left)
  19. self.flatten(root.right)
  20. if root.left == None: # it means we do not need to do anything
  21. return
  22. p = root.left # go to the bottom of root's left tree
  23. while p.right:
  24. p = p.right
  25. p.right = root.right
  26. root.right = root.left
  27. root.left = None
Add Comment
Please, Sign In to add comment