[Leetcode] Construct binary tree from inorder and postorder travesal 利用中序和后续遍历构造二叉树

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

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

利用中序和后序遍历构造二叉树,要注意到后序遍历的最后一个元素是二叉树的根节点,而中序遍历中,根节点前面为左子树节点后面为右子树的节点。例如二叉树:{1,2,3,4,5,6,#}的后序遍历为4->5->2->6->3->1;中序遍历为:4->2->5->1->6->3。

故,递归的整体思路是,找到根节点,然后遍历左右子树。

方法一:递归

值得注意的是:递归过程中,起始点的选取。Grandyang对下标的选取有较为详细的说明。

 1 /**
 2  * Definition for binary tree
 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     {
14         int len=inorder.size();
15         return build(inorder,0,len-1,postorder,0,len-1);
16     }
17     TreeNode *build(vector<int> &inorder,int inBeg,int inEnd,vector<int> &postorder,int postBeg,int postEnd)
18     {
19         if(inBeg>inEnd||postBeg>postEnd)    return NULL;
20         TreeNode *root=new TreeNode(postorder[postEnd]);
21
22         for(int i=0;i<postorder.size();++i)
23         {
24             if(inorder[i]==postorder[postEnd])
25             {
26                 root->left=build(inorder,inBeg,i-1,postorder,postBeg,postBeg+i-1-inBeg);
27                 root->right=build(inorder,i+1,inEnd,postorder,postBeg+i-inBeg,postEnd-1);
28             }
29         }
30         return root;
31     }
32 };

方法二:

利用栈。这个方法是以前看到出处也尴尬的忘了,分析过程等我再次看懂了再补上。这种方法改变了数组。

 1 /**
 2  * Definition for binary tree
 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     {
14         if(inorder.size()==0)   return NULL;
15         TreeNode* p;
16         TreeNode* root;
17         stack<TreeNode*> stn;
18
19         root=new TreeNode(postorder.back());
20         stn.push(root);
21         postorder.pop_back();
22
23         while(true)
24         {
25             if(inorder.back()==stn.top()->val)
26             {
27                 p=stn.top();
28                 stn.pop();
29                 inorder.pop_back();
30
31                 if(inorder.size()==0)   break;
32                 if(stn.size()&&inorder.back()==stn.top()->val)
33                     continue;
34
35                 p->left=new TreeNode(postorder.back());
36                 postorder.pop_back();
37                 stn.push(p->left);
38             }
39             else
40             {
41                 p=new TreeNode(postorder.back());
42                 postorder.pop_back();
43                 stn.top()->right=p;
44                 stn.push(p);
45             }
46         }
47         return root;
48     }
49 };

时间: 2024-09-30 07:47:57

[Leetcode] Construct binary tree from inorder and postorder travesal 利用中序和后续遍历构造二叉树的相关文章

【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal-通过中序和后续遍历还原二叉树

一.描述: 二.思路: 二叉树的中序遍历和前序遍历或和后续遍历能唯一确定一节课二叉树,即2中还原方式都需要中序遍历才能完成: 设二叉树的前序遍历序列为{1, 2, 4, 5, 3, 6},中序遍历序列为{4,2,5,1, 3, 6}:(红色标记表示以还原节点!!!) (1)-前序遍历的第一个节点是二叉树的根节点,{1, 2, 4, 5, 3, 6},对应中序中的位置是{4,2,5,1, 3, 6},所以中序序列中的 '1' 之前的全部元素为左子树元素,'1'之后的为右子树元素: (2)-左子树对

[LeetCode] 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. 这道题要求从中序和后序遍历的结果来重建原二叉树,我们知道中序的遍历顺序是左-根-右,后序的顺序是左-右-根,对于这种树的重建一般都是采用递归来做,可参见我之前的一篇博客Convert Sorted Array to Bin

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

Construct Binary Tree from Inorder and Postorder Traversal ——通过中序、后序遍历得到二叉树

题意:根据二叉树的中序遍历和后序遍历恢复二叉树. 解题思路:看到树首先想到要用递归来解题.以这道题为例:如果一颗二叉树为{1,2,3,4,5,6,7},则中序遍历为{4,2,5,1,6,3,7},后序遍历为{4,5,2,6,7,3,1},我们可以反推回去.由于后序遍历的最后一个节点就是树的根.也就是root=1,然后我们在中序遍历中搜索1,可以看到中序遍历的第四个数是1,也就是root.根据中序遍历的定义,1左边的数{4,2,5}就是左子树的中序遍历,1右边的数{6,3,7}就是右子树的中序遍历

[LeetCode] 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 *buildTree(vector<int> &inorder, vector<int> &postorder) { int

[leetcode]Construct Binary Tree from Inorder and Postorder Traversal @ Python

原题地址:http://oj.leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/ 题意:根据二叉树的中序遍历和后序遍历恢复二叉树. 解题思路:看到树首先想到要用递归来解题.以这道题为例:如果一颗二叉树为{1,2,3,4,5,6,7},则中序遍历为{4,2,5,1,6,3,7},后序遍历为{4,5,2,6,7,3,1},我们可以反推回去.由于后序遍历的最后一个节点就是树的根.也就是roo

LeetCode: Construct Binary Tree from Inorder and Postorder Traversal 解题报告

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. Hide Tags Tree Array Depth-first Search SOLUTION 1: 使

[Leetcode] Construct Binary Tree from Inorder and Postorder Traversal I,II

这两个问题实际上是同一个问题,需要对三种遍历方式的规律非常清楚. 对于前序遍历,第一个元素实际上就是root,然后后面的元素前半部分是左树的node,后半部分是右树的node 对于中序遍历,一旦我们知道了root节点,那么就可以将其分为两半部分,也就是左树和右树 对于后序遍历,我们可以缺点最后一个节点是root节点,剩下的部分:前半部分是左树节点,后半部分是右树节点. 这样使用递归就可以解决,需要使用两组index划定两种遍历的range,当left>right的时候,就需要返回null 有点类