zoukankan      html  css  js  c++  java
  • LintCode: Flatten Binary Tree to Linked List

    C++

    Traverse

     1 /**
     2  * Definition of TreeNode:
     3  * class TreeNode {
     4  * public:
     5  *     int val;
     6  *     TreeNode *left, *right;
     7  *     TreeNode(int val) {
     8  *         this->val = val;
     9  *         this->left = this->right = NULL;
    10  *     }
    11  * }
    12  */
    13 class Solution {
    14 public:
    15     /**
    16      * @param root: a TreeNode, the root of the binary tree
    17      * @return: nothing
    18      */
    19     void flatten(TreeNode *root) {
    20         if(!root) return;
    21         vector<TreeNode*> allNodes;
    22         preorder(root, allNodes);
    23         int n = allNodes.size();
    24         for(int i=0; i<n-1; i++) {
    25             allNodes[i]->left = NULL;
    26             allNodes[i]->right = allNodes[i+1];
    27         }
    28         allNodes[n-1]->left = allNodes[n-1]->right = NULL;
    29     }
    30     
    31     void preorder(TreeNode *root, vector<TreeNode*> &allNodes) {
    32         if(!root) return;
    33         allNodes.push_back(root);
    34         preorder(root->left, allNodes);
    35         preorder(root->right, allNodes);
    36     }
    37 };

    C++

    Divide-Conquer

    Recursive

     1 /**
     2  * Definition of TreeNode:
     3  * class TreeNode {
     4  * public:
     5  *     int val;
     6  *     TreeNode *left, *right;
     7  *     TreeNode(int val) {
     8  *         this->val = val;
     9  *         this->left = this->right = NULL;
    10  *     }
    11  * }
    12  */
    13 class Solution {
    14 public:
    15     /**
    16      * @param root: a TreeNode, the root of the binary tree
    17      * @return: nothing
    18      */
    19     void flatten(TreeNode *root) {
    20         helper(root);
    21     }
    22     
    23     TreeNode* helper(TreeNode *root) {
    24         if (root == NULL) {
    25             return NULL;
    26         }
    27         if (root->left == NULL && root->right == NULL) {
    28             return root;
    29         }
    30         if (root->left == NULL) {
    31             return helper(root->right);
    32         }
    33         if (root->right == NULL) {
    34             root->right = root->left;
    35             root->left = NULL;//!important
    36             return helper(root->right);
    37         }
    38         //Divide
    39         TreeNode *leftLastNode = helper(root->left);
    40         TreeNode *rightLastNode = helper(root->right);
    41         
    42         //Conquer
    43         leftLastNode->right = root->right;
    44         root->right = root->left;
    45         root->left = NULL;//!important
    46         return rightLastNode;
    47     }
    48 };
  • 相关阅读:
    暴力+构造 Codeforces Round #283 (Div. 2) C. Removing Columns
    Help Jimmy ~poj-1661 基础DP
    POJ1015 && UVA
    FatMouse's Speed ~(基础DP)打印路径的上升子序列
    Max Sum Plus Plus
    Column Addition~DP(脑子抽了,当时没有想到)
    区间的连续段~ST表(模板题)
    Exponial~(欧拉函数)~(发呆题)
    wyh的数列~(坑爹题目)
    wyh的物品~(二分)
  • 原文地址:https://www.cnblogs.com/CheeseZH/p/5012685.html
Copyright © 2011-2022 走看看