zoukankan      html  css  js  c++  java
  • Leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal

    106. Construct Binary Tree from Inorder and Postorder Traversal

    • Total Accepted: 60461
    • Total Submissions: 203546
    • Difficulty: Medium

    Given inorder and postorder traversal of a tree, construct the binary tree.

    Note:
    You may assume that duplicates do not exist in the tree.

    Subscribe to see which companies asked this question

    思路:由树的中序和后序建树,可以用递归和迭代。

    代码:
    递归形式:156 ms

    想法比较直白,就是递归建树。

     1  /**
     2  * Definition for a binary tree node.
     3  * struct TreeNode {
     4  *     int val;
     5  *     TreeNode *left;
     6  *     TreeNode *right;
     7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     8  * };
     9  */
    10 class Solution {
    11 public:
    12     TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {//中序和后序
    13         if(postorder.size()==0){
    14             return NULL;
    15         }
    16         int i,n=postorder.size();
    17         TreeNode* root=new TreeNode(postorder.back());
    18         vector<int> rightinorder,rightpostorder;
    19         for(i=0;i<n&&inorder[i]!=postorder.back();i++);
    20         inorder.erase(inorder.begin()+i);
    21         postorder.pop_back();
    22         while(i<inorder.size()){
    23             rightinorder.push_back(inorder[i]);
    24             inorder.erase(inorder.begin()+i);
    25             rightpostorder.push_back(postorder[i]);
    26             postorder.erase(postorder.begin()+i);
    27         }
    28         root->left=buildTree(inorder,postorder);
    29         root->right=buildTree(rightinorder,rightpostorder);
    30         return root;
    31     }
    32 };

    迭代形式:28 ms

    参考:https://discuss.leetcode.com/topic/4746/my-comprehension-of-o-n-solution-from-hongzhi/2

    原文如下:

    Below is the O(n) solution from @hongzhi but that discuss is closed now 'cause @hongzhi says little about his code.
    
    https://oj.leetcode.com/discuss/6334/here-is-my-o-n-solution-is-it-neat
    
    I've modified some of and tried this code and got AC.
    Just share about some comprehension about his code.
    
    I've modified vtn(vector) to stn(stack) in that stack is probably what this algs means and needs.
    
    What matters most is the meaning of stn.
    
    Only nodes whoes left side hasn't been handled will be pushed into stn.
    
    And inorder is organized as (inorder of left) root (inorder of right),
    
    And postorder is as (postorder of left) (postorder of right) root.
    
    So at the very begin, we only have root in stn and we check if inorder.back() == root->val and in most cases it's false(see Note 1). Then we make this node root's right sub-node and push it into stn.
    
    Note 1: this is actually (inorder of right).back() == (postorder of right).back(), so if only there's no right subtree or the answer will always be false.
    
    Note 2: we delete one node from postorder as we push one into stn.
    
    Now we have [root, root's right] as stn and we check inorder.back() == stn.top()->val again.
    
    true means inorder.back() is the root node and needs handled left case.
    false means inorder.back() is the next right sub-node
    So when we encounter a true, we will cache stn.top() as p and delete both nodes from inorder and stn.
    
    Then we check inorder.size(), if there's no nodes left, it means p has no left node.
    
    Else the next node in inorder could be p's left node or p's father which equals to the now stn.top() (remember we popped p from stn above).
    
    If the latter happens, it means p has no left node and we need to move on to p's father(stn.top()).
    
    If the former happens, it means p has one left node and it's postorder.back(), so we put it to p's left and delete it from the postorder and push the left node into stn 'cause it should be the next check node as the postorder is organized as above.
     1 /**
     2  * Definition for a binary tree node.
     3  * struct TreeNode {
     4  *     int val;
     5  *     TreeNode *left;
     6  *     TreeNode *right;
     7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     8  * };
     9  */
    10 class Solution {
    11 public:
    12     TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {//中序和后序
    13         stack<TreeNode*> s;
    14         if(postorder.size()==0){
    15             return NULL;
    16         }
    17         TreeNode* root=new TreeNode(postorder.back());
    18         TreeNode* p;
    19         postorder.pop_back();
    20         s.push(root);
    21         while(true){
    22             if(s.top()->val==inorder.back()){
    23                 p=s.top();
    24                 s.pop();
    25                 inorder.pop_back();
    26                 if(inorder.size()==0) break;
    27                 if(!s.empty()&&inorder.back()==s.top()->val) continue;//p是否还有左子树
    28                 TreeNode* temp=new TreeNode(postorder.back());
    29                 p->left=temp;
    30                 s.push(temp);
    31                 postorder.pop_back();
    32             }
    33             else{
    34                 TreeNode* temp=new TreeNode(postorder.back());
    35                 s.top()->right=temp;
    36                 s.push(temp);
    37                 postorder.pop_back();
    38             }
    39         }
    40         return root;
    41     }
    42 };
  • 相关阅读:
    Comparable内部比较器 和 Comparator外部比较器
    java——包装类数据缓存 ==号详解
    java——包装类中的equals方法
    Eclipse怎么改变@author 姓名
    非常简约学生管理系统——HashSet进行编写
    TreeMap——实现comparable接口并重写CompareTo方法
    flume收集日志直接sink到oracle数据库
    大文件多个服务器复制拷贝
    oracle创建用户表空间
    缓存策略:redis缓存之springCache
  • 原文地址:https://www.cnblogs.com/Deribs4/p/5699490.html
Copyright © 2011-2022 走看看