1. 程式人生 > >leetcode: 114. Flatten Binary Tree to Linked List [✗]

leetcode: 114. Flatten Binary Tree to Linked List [✗]

Difficulty

Medium.

Problem

Given a binary tree, flatten it to a linked list in-place.

For example, given the following tree:

    1
   / \
  2   5
 / \   \
3   4   6
The flattened tree should look like:

1
 \
  2
   \
    3
     \
      4
       \
        5
         \
          6

AC

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 None self.recurse(root) def recurse(self, root): # return the head and tail of flattened list
if not root.left and not root.right: return root, root if not root.left and root.right: r, last = self.recurse(root.right) return root, last elif not root.right and root.left: l, last = self.recurse(root.left) root.
right = l root.left = None return root, last else: l, llast = self.recurse(root.left) r, rlast= self.recurse(root.right) root.right = l llast.right = r rlast.right = None return root, rlast