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 };
时间: 2024-08-05 07:09:31

Leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal的相关文章

Java for LeetCode 106 Construct Binary Tree from Inorder and Postorder Traversal

Construct Binary Tree from Inorder and Postorder Traversal Total Accepted: 31041 Total Submissions: 115870 Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. 解题思路

LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal (用中序和后序树遍历来建立二叉树)

Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. 题目标签:Array, Tree 这到题目和105 几乎是一摸一样的,唯一的区别就是把pre-order 换成 post-order.因为post-order是最后一个数字是root,所以要从右向左的遍历.还需要把helpe

C#解leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal

Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. 这个题目是给你一棵树的中序遍历和后序遍历,让你将这棵树表示出来.其中可以假设在树中没有重复的元素. 当做完这个题之后,建议去做做第105题,跟这道题类似. 分析:这个解法的基本思想是:我们有两个数组,分别是IN和POST.后

leetCode 106.Construct Binary Tree from Inorder and Postorder Traversal (根据中序遍历和后序遍历构造二叉树)

Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. 思路:这题和上题类似,前序第一个是根节点,后序遍历最后一个是根节点.其余步骤类似. 代码如下: /** * Definition for a binary tree node. * public class TreeNod

LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树 C++

Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. For example, given inorder = [9,3,15,20,7] postorder = [9,15,7,20,3] Return the following binary tree: 3 / 9 20

leetcode -day23 Construct Binary Tree from Inorder and Postorder Traversal &amp; Construct Binary Tree f

1.  Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. 代码: class Solution { public: TreeNode *buildTr

LeetCode | 0106. Construct Binary Tree from Inorder and Postorder Traversal从中序与后序遍历序列构造二叉树【Python】

LeetCode 0106. Construct Binary Tree from Inorder and Postorder Traversal从中序与后序遍历序列构造二叉树[Medium][Python][二叉树][递归] Problem LeetCode Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not

Leetcode dfs Construct Binary Tree from Inorder and Postorder Traversal

Construct Binary Tree from Inorder and Postorder Traversal Total Accepted: 14363 Total Submissions: 54254My Submissions Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in t

leetcode -day23 Construct Binary Tree from Inorder and Postorder Traversal &amp;amp; Construct Binary Tree f

1.  Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. 代码: class Solution { public: TreeNode *buildTr