码迷,mamicode.com
首页 > 其他好文 > 详细

Leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal

时间:2016-07-23 22:43:22      阅读:270      评论:0      收藏:0      [点我收藏+]

标签:

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

Ive modified some of and tried this code and got AC.
Just share about some comprehension about his code.

Ive 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 hasnt 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 its false(see Note 1). Then we make this node roots right sub-node and push it into stn.

Note 1: this is actually (inorder of right).back() == (postorder of right).back(), so if only theres 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, roots 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 theres no nodes left, it means p has no left node.

Else the next node in inorder could be ps left node or ps 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 ps father(stn.top()).

If the former happens, it means p has one left node and its postorder.back(), so we put it to ps 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 };

 

Leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal

标签:

原文地址:http://www.cnblogs.com/Deribs4/p/5699490.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!