Flatten Binary Tree to Linked List

2014.2.13 01:03

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

For example,
Given

         1
        / \
       2   5
      / \   \
     3   4   6

The flattened tree should look like:

   1
    \
     2
      \
       3
        \
         4
          \
           5
            \
             6

click to show hints.

Hints:

If you notice carefully in the flattened tree, each node's right child points to the next node of a pre-order traversal.

Solution:

  The easy way to solve this problem is by postorder traversal of this tree:

    1. Flatten the left subtree.

    2. Flatten the right subtree.

    3. Connect the root->left subtree->right subtree.

  When the left and right subtrees are both flattened, piece them together with the root node to make a complete left-skewed tree.

  When connecting them, we have to know the boundary nodes of each part. This can be done by passing two parameters as the left and right borders of the tree. Their values are updated as the recursions are performed.

  Total time complexity is O(n). Space complexity is O(n) as well.

  As for the non-recursive solution of this problem, well...maybe later.

Accepted code:

 1 // 4WA, 1AC, not so easy.
 2 /**
 3  * Definition for binary tree
 4  * struct TreeNode {
 5  *     int val;
 6  *     TreeNode *left;
 7  *     TreeNode *right;
 8  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 9  * };
10  */
11 class Solution {
12 public:
13     void flatten(TreeNode *root) {
14         if (root == nullptr) {
15             return;
16         }
17         
18         if (root->left == nullptr && root->right == nullptr) {
19             return;
20         }
21         
22         TreeNode *left_most, *right_most;
23         
24         flattenRecursive(root, left_most, right_most);
25     }
26 private:
27     void flattenRecursive(TreeNode *root, TreeNode *&left_most, TreeNode *&right_most) {
28         if (root->left == nullptr && root->right == nullptr) {
29             left_most = root;
30             right_most = root;
31             return;
32         }
33         
34         TreeNode *ll, *lr, *rl, *rr;
35         
36         if (root->left != nullptr) {
37             flattenRecursive(root->left, ll, lr);
38         } else {
39             ll = lr = root;
40         }
41         if (root->right != nullptr) {
42             flattenRecursive(root->right, rl, rr);
43         } else {
44             rl = rr = root;
45         }
46         
47         TreeNode *ptr;
48         root->left = nullptr;
49         if (ll != root) {
50             root->right = ll;
51             lr->right = nullptr;
52             ptr = lr;
53         } else {
54             ptr = root;
55         }
56         if (rr != root) {
57             ptr->right = rl;
58         }
59         
60         left_most = root;
61         if (rr != root) {
62             right_most = rr;
63         } else {
64             right_most = lr;
65         }
66     }
67 };

 

 posted on 2014-02-13 01:22  zhuli19901106  阅读(188)  评论(0编辑  收藏  举报