(二叉树 递归) 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.
For example, given
inorder = [9,3,15,20,7]
postorder = [9,15,7,20,3]
Return the following binary tree:
3
/ \
9 20
/ \
15 7 -------------------------------------------------------------------------------------
就是从中序遍历和后序遍历构建二叉树。可以用递归方式。注意递归的终止条件。
和leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal几乎一样。 参考博客:http://www.cnblogs.com/grandyang/p/4296193.html C++代码:
/**
* Definition for a binary tree node.
* 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) {
return build(inorder,,inorder.size()-,postorder,,postorder.size() - );
}
TreeNode *build(vector<int> &inorder,int ileft,int iright,vector<int> &postorder,int pleft,int pright){
if(ileft > iright ||pleft > pright) return NULL; //终止条件,就是当序列的长度为0时,递归终止。
int i = ;
TreeNode *cur = new TreeNode(postorder[pright]);
for(i = ileft; i < inorder.size(); i++){
if(inorder[i] == cur->val)
break;
}
cur->left = build(inorder,ileft,i-,postorder,pleft,pleft + i - ileft - );
cur->right = build(inorder,i + ,iright,postorder,pleft + i - ileft,pright - );
return cur;
}
};
还有一个方法,就是建立几个数组,保存分割后的数组。不过时间会很长。
C++代码:
/**
* Definition for a binary tree node.
* 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) {
return build(inorder,postorder);
}
TreeNode *build(vector<int> &inorder,vector<int> &postorder){
if(inorder.size() == || postorder.size() == ) //递归条件,当然也可以加上if(inorder.size() == 1 || postorder.size() == 1)return cur;这个递归条件。
return NULL;
int rootval = postorder.back();
TreeNode *cur = new TreeNode(rootval);
int i = ;
for(i = ; i < inorder.size(); i++){
if(inorder[i] == rootval) break;
}
vector<int> inleft,inright;
vector<int> poleft,poright;
for(int j = ; j < i; j++){
inleft.push_back(inorder[j]);
poleft.push_back(postorder[j]);
}
for(int j = i + ; j < inorder.size(); j++){
inright.push_back(inorder[j]);
}
for(int j = i; j < postorder.size() - ; j++){
poright.push_back(postorder[j]);
}
cur->left = build(inleft,poleft);
cur->right = build(inright,poright);
return cur;
}
};
这两个方法从算法上看是一样的,只是代码的实现不同而已。
(二叉树 递归) 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: ...
- [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 (用中序和后序树遍历来建立二叉树)
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 ...
- 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 ...
- (二叉树 递归) leetcode 889. Construct Binary Tree from Preorder and Postorder Traversal
Return any binary tree that matches the given preorder and postorder traversals. Values in the trave ...
- Leetcode#106 Construct Binary Tree from Inorder and Postorder Traversal
原题地址 二叉树基本操作 [ ]O[ ] [ ][ ]O 代码: TreeNode *restore(vector<i ...
- [leetcode] 106. Construct Binary Tree from Inorder and Postorder Traversal(medium)
原题地址 思路: 和leetcode105题差不多,这道题是给中序和后序,求出二叉树. 解法一: 思路和105题差不多,只是pos是从后往前遍历,生成树顺序也是先右后左. class Solution ...
- 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告
[LeetCode]106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告(Python) 标签: LeetCode ...
随机推荐
- 【温故而知新】HTTP 报文
HTTP 报文是在 HTTP 应用程序之间发送的数据块.这些数据块以一些文本形式的元信息开头,这些信息描述了报文的内容及含义. 报文流 报文在客户端.服务器和代理之间的流动称为报文流. HTTP 使用 ...
- python正则表达式模块re
正则表达式的特殊元素 匹配符号 描述 '.'(点dot) 在默认模式下,它匹配除换行符之外的任何字符.如果指定了DOTALL标志,则匹配包括换行符在内的任何字符 '^'(Caret) 匹配以字符串开头 ...
- wxPython树控件
1.树控件 树(tree)是一种通过层次结构展示信息的控件,如下图所示是树控件示例,左窗口中是树控件,在wxPython中树控件类是wx.TreeCtrl. wx.TreeCtrl中一个常用的方法有: ...
- 转:[kipmi0]进程导致系统负载高
最近一个用户这边服务器出现服务器负载很高的情况,原本正常是0.3~0.5左右 不正常的时候会达到3,重启机器就正常,开始以为是程序问题,后来在观察的时候把程序给杀掉了 然后重启,结果负载还是很高,于 ...
- docker容器日志收集方案(方案N,其他中间件传输方案)
由于docker虚拟化的特殊性导致日志收集方案的多样性和复杂性下面接收几个可能的方案 这个方案各大公司都在用只不过传输方式大同小异 中间件使用kafka是肯定的,kafka的积压与吞吐能力相当强悍 ...
- 【English EMail】Compensation Planning Memo
Data Foundation 数据基础 [faʊnˈdeʃən] Interesting newsletter for data foundation practice. Annual Code ...
- Failed to start /etc/rc.d/rc.local Compatibility
查看/var/log/message Jun :: root systemd: Started Network Manager. Jun :: root systemd: Starting LSB: ...
- HTML DOM 事件对象 ondragend 事件
学习网站:http://www.runoob.com/jsref/event-ondragend.html 定义和用法 ondragend 事件在用户完成元素或首选文本的拖动时触发. 拖放是 HTML ...
- web框架开发-Django组件cookie与session
http协议的每一次都是无保存状态的请求,这会带来很多的不方便,比如,一刷新网页,或者进入该网页的其他页面,无法保存之前的登录状态.为了解决类似这样的问题,引入了会话跟踪 会话跟踪技术 1 什么是会话 ...
- day5-python的文件操作-坚持就好
目录摘要 文件处理 1.文件初识 2.文件的读操作 3.文件的写操作 4.文件的追加操作 5.文件的其他操作 6.文件的修改 正式开始 文件处理:写了这么多代码了,有的时候我们执行完成的结果想永久保存 ...