zoukankan      html  css  js  c++  java
  • LeetCode

    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 };
  • 相关阅读:
    一个小案例精通lamda表达式
    你想被开除吗?来看看程序员【离职小技巧】吧
    让 Flutter 在鸿蒙系统上跑起来
    “TensorFlow 开发者出道计划”全攻略,玩转社区看这里!
    环形单链表的增删改查、约瑟夫环两种解法
    一万字详解 Redis Cluster Gossip 协议
    Lambda表达式
    Linux系统中如何进入退出vim编辑器,方法及区别
    成为博客主的第一天
    【秋招内推】近期互联网公司秋招内推合集
  • 原文地址:https://www.cnblogs.com/zhuli19901106/p/3547406.html
Copyright © 2011-2022 走看看