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 traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree. 本文地址
分析:后序序列的最后一个元素就是树根,然后在中序序列中找到这个元素(由于题目保证没有相同的元素,因此可以唯一找到),中序序列中这个元素的左边就是左子树的中序,右边就是右子树的中序,然后根据刚才中序序列中左右子树的元素个数可以在后序序列中找到左右子树的后序序列,然后递归的求解即可
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
typedef vector<int>::iterator Iter;
TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
return buildTreeRecur(inorder.begin(), inorder.end(), postorder.begin(), postorder.end());
}
TreeNode *buildTreeRecur(Iter istart, Iter iend, Iter pstart, Iter pend)
{
if(istart == iend)return NULL;
int rootval = *(pend-);
Iter iterroot = find(istart, iend, rootval);
TreeNode *res = new TreeNode(rootval);
res->left = buildTreeRecur(istart, iterroot, pstart, pstart+(iterroot-istart));
res->right = buildTreeRecur(iterroot+, iend, pstart+(iterroot-istart), pend-);
return res;
}
};
LeetCode:Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree
同上,只是树根是先序序列的第一个元素
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
typedef vector<int>::iterator Iter;
TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
return buildTreeRecur(inorder.begin(), inorder.end(), preorder.begin(), preorder.end());
}
TreeNode *buildTreeRecur(Iter istart, Iter iend, Iter pstart, Iter pend)
{
if(istart == iend)return NULL;
int rootval = *pstart;
Iter iterroot = find(istart, iend, rootval);
TreeNode *res = new TreeNode(rootval);
res->left = buildTreeRecur(istart, iterroot, pstart+, pstart++(iterroot-istart));
res->right = buildTreeRecur(iterroot+, iend, pstart++(iterroot-istart), pend);
return res;
}
};
【版权声明】转载请注明出处:http://www.cnblogs.com/TenosDoIt/p/3440560.html
LeetCode:Construct Binary Tree from Inorder and Postorder Traversal,Construct Binary Tree from Preorder and Inorder Traversal的更多相关文章
- 【LeetCode OJ】Construct Binary Tree from Inorder and Postorder Traversal
Problem Link: https://oj.leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-trav ...
- 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 ...
- 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...
- leetcode-1006 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-21]Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that ...
- [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 ...
- 【LeetCode】105 & 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 ...
- 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 Traversall
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源:http ...
随机推荐
- jQuery学习笔记:整理一些常用的jQuery操作DOM事件
1.attr() .removeAttr() .attr() 方法可以传入一个名值对的参数,也可以传入一个包含2个以上名值对的对象参数,例如: .attr('src','images/a.jpg'); ...
- 读书笔记——Windows环境下32位汇编语言程序设计(6)使用浮点指令进行64位除法
罗云彬 典藏版Page192,mark下. 这段代码看不懂,手册上根本没有fdivr不带操作数的指令. .data dqTickCounter1 dq ? dqTickCounter2 dq ? dq ...
- IE6-9中tbody的innerHTML不能赋值bug
IE6-IE9中tbody的innerHTML不能赋值,重现代码如下 <!DOCTYPE html> <html> <head> <meta charset= ...
- 数据结构--线段树--lazy延迟操作
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 53749 ...
- splice()函数,'SPLICE_F_MOVE' 'SPLICE_F_NONBLOCK' 'SPLICE_F_MORE' undeclared
1.编译含有splice()函数的程序时出现,'SPLICE_F_MOVE' undeclared,'SPLICE_F_NONBLOCK' ‘SPLICE_F_MORE' 也是一样undeclare ...
- 备忘:文本编辑器(z.B. Sublime Text 2)策略,git策略
1.以Sublime Text 2 为例: 新建一个test.py文件,敲完例程 代码 之后,再另存为比如 if.py, list_tuple.py云云 而test.py可以一直用来编辑 2.git ...
- Hive get table rows count batch
项目中需要比对两种方法计算生成的数据情况,需要做两件事情,比对生成的中间表的行数是否相同,比对最后一张表的数据是否一致. 在获取表的数据量是一条一条地使用select count(*) from ta ...
- HTML常用文本元素
HTML是超文本标记语言,它提供网页的具体内容,包括文本.表单.图像.表格.链接.多媒体.列表等.其中文本是我们遇到的最多的展示内容.正确的使用文本标签,会使页面具有语义化,也有利于SEO. 文本标签 ...
- 【ASP.NET 进阶】根据IP地址进行百度地图定位
昨天有完成一个[ASP.NET 进阶]根据IP返回对应位置信息 的小Demo,既然可以通过IP获得位置信息,那当然可以通过位置信息的经纬度获取IP的当前定位了,虽然与实际地址偏移较大,毕竟不是GPRS ...
- 边工作边刷题:70天一遍leetcode: day 88-5
coins in a line I/II/III: check above 1. recursion的返回和dp[left][right]表示什么?假设game是[left,right],那么play ...