[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对下标的选取有较为详细的说明。至于这个递归过程,(个人愚见,欢迎大神给出更好递归过程)可以在脑海想,构造顺序为4->5->2构建好左子树,接上1,然后6->3构建好右子树,最后接上1,完成。
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder)
{
int len=inorder.size();
return build(inorder,,len-,postorder,,len-);
}
TreeNode *build(vector<int> &inorder,int inBeg,int inEnd,vector<int> &postorder,int postBeg,int postEnd)
{
if(inBeg>inEnd||postBeg>postEnd) return NULL;
TreeNode *root=new TreeNode(postorder[postEnd]); for(int i=;i<inorder.size();++i)
{
if(inorder[i]==postorder[postEnd])
{
root->left=build(inorder,inBeg,i-,postorder,postBeg,postBeg+i--inBeg);
root->right=build(inorder,i+,inEnd,postorder,postBeg+i-inBeg,postEnd-);
}
}
return root;
}
};
方法二:
利用栈。这个方法是以前看到出处也尴尬的忘了,分析过程等我再次看懂了再补上。这种方法改变了数组。
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder)
{
if(inorder.size()==) return NULL;
TreeNode* p;
TreeNode* root;
stack<TreeNode*> stn; root=new TreeNode(postorder.back());
stn.push(root);
postorder.pop_back(); while(true)
{
if(inorder.back()==stn.top()->val)
{
p=stn.top();
stn.pop();
inorder.pop_back(); if(inorder.size()==) break;
if(stn.size()&&inorder.back()==stn.top()->val)
continue; p->left=new TreeNode(postorder.back());
postorder.pop_back();
stn.push(p->left);
}
else
{
p=new TreeNode(postorder.back());
postorder.pop_back();
stn.top()->right=p;
stn.push(p);
}
}
return root;
}
};
[Leetcode] Construct binary tree from inorder and postorder travesal 利用中序和后续遍历构造二叉树的更多相关文章
- [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 tha ...
- Construct Binary Tree from Inorder and Postorder Traversal(根据中序遍历和后序遍历构建二叉树)
根据中序和后续遍历构建二叉树. /** * Definition for a binary tree node. * public class TreeNode { * int val; * 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 ...
- 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 ...
- Construct Binary Tree from Inorder and Postorder Traversal ——通过中序、后序遍历得到二叉树
题意:根据二叉树的中序遍历和后序遍历恢复二叉树. 解题思路:看到树首先想到要用递归来解题.以这道题为例:如果一颗二叉树为{1,2,3,4,5,6,7},则中序遍历为{4,2,5,1,6,3,7},后序 ...
- LeetCode: 106_Construct Binary Tree from Inorder and Postorder Traversal | 根据中序和后序遍历构建二叉树 | Medium
要求:根据中序和后序遍历序列构建一棵二叉树 代码如下: struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int ...
- LeetCode:Construct Binary Tree from Inorder and Postorder Traversal,Construct Binary Tree from Preorder and Inorder Traversal
LeetCode:Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder trav ...
- LeetCode: Construct Binary Tree from Inorder and Postorder Traversal 解题报告
Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...
- 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 ...
随机推荐
- vim程序员加强功能
1.折叠 1.1折叠的方式有六种 manual:以标准的vim结构定义折叠跨越的范围,类似移动命令 indent:折叠与折叠的层次,对应于文本的缩排与 ...
- 你需要掌握的http知识
作为一名前端er,http是我们必须要掌握的,那么我们到底需要掌握哪些东西呢 一.基础知识 这里我们介绍与http相关的TCP.IP.DNS.url.uri 1.IP IP地址是我们很熟悉的东西,最常 ...
- hdu1217Arbitrage(floyd+map)
Arbitrage Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total S ...
- logisitic回归
线性回归目的是找到一条直线(或者超平面)尽可能地接近所有的训练数据点,而对数几率回归的目的是找到一条直线(或者超平面)尽可能地分开两种不同类别的数据点. 对数几率回归感觉更像是一个分类问题.https ...
- Linux error:No space left on device
一台Oracle数据库服务器在关机重启后,Oracle监听无法启动,提示错误 Linux error:no space left on device 提示可知:问题是出在磁盘空间不足 但是初步查看分区 ...
- GitHub把自己整个文件夹上传
我已经有了自己github,但是我怎么对我的项目进行上传呢,普通的上传只有上传单一的文件 这不我去下载了Git(链接至机房ftp文件夹下文件ftp://10.64.130.1/%C8%ED%BC%FE ...
- C#中委托的发展与匿名函数
匿名函数(C# 编程指南) 匿名函数是一个“内联”语句或表达式,可在需要委托类型的任何地方使用. 可以使用匿名函数来初始化命名委托,或传递命名委托(而不是命名委托类型)作为方法参数. 共有两种匿名函数 ...
- Java容器之Map接口
Map 接口: 1. 实现 Map 接口的类是用来存储键-值(key-value)对: 2. Map 接口的实现类有 HashMap 和 TreeMap 等: 3. Map 类中存储的键-值对,通过键 ...
- autoCAD 2008 Win7 64位, win8 64位 安装 燕秀工具箱 yanxiu.cui 文件下载
Win7 64位, win8 64位 安装 燕秀工具箱 , 提示没有权限. 网站上下载燕秀工具箱, 安装后. 提示权限不够. 解决办法如下; 1. CAD, 权限修改. 2. 下载 yanxiu.cu ...
- C++并行编程1
what is concurrency 我们可以一边看电视,一边唱歌.人并行非常容易理解,但是计算机呢?是不是我们一边编辑着word文档,一边听着歌,这样计算机就是在并行吗?不一定欧,如果你计算机是单 ...