【Construct Binary Tree from Preorder and Inorder Traversal】cpp
题目:
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 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>& preorder, vector<int>& inorder) {
if ( preorder.size()== || inorder.size()== ) return NULL;
return Solution::buildTreePI(preorder, , preorder.size()-, inorder, , inorder.size()-);
}
static TreeNode* buildTreePI(
vector<int>& preorder,
int beginP, int endP,
vector<int>& inorder,
int beginI, int endI)
{
// terminal condition & corner case
if ( beginP>endP ) return NULL;
// resurisve process
TreeNode *root = new TreeNode(-);
root->val = preorder[beginP];
// find the root node in inorder traversal
int rootPosInorder = beginI;
for ( int i = beginI; i <= endI; ++i )
{
if ( inorder[i]==root->val ) { rootPosInorder=i; break;}
}
int leftSize = rootPosInorder - beginI;
int rightSize = endI - rootPosInorder;
root->left = Solution::buildTreePI(preorder, beginP+, beginP+leftSize, inorder, beginI, rootPosInorder-);
root->right = Solution::buildTreePI(preorder, endP-rightSize+, endP, inorder, rootPosInorder+, endI);
return root;
}
};
tips:
经典题目。直接学习高手代码
http://fisherlei.blogspot.sg/2013/01/leetcode-construct-binary-tree-from.html
http://bangbingsyb.blogspot.sg/2014/11/leetcode-construct-binary-tree-from.html
这里注意在递归传递vector元素下标的时候,一定是绝对下标(一开始疏忽写成了相对下标,debug了不少时间)
=========================================
第二次过这道题,大体思路还记得,顺着思路摸了下来,代码改了一次AC了。注意每次要找到的是preorder[bp]而不是preorder[0]。
/**
* 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>& preorder, vector<int>& inorder)
{
return Solution::build(preorder, , preorder.size()-, inorder, , inorder.size()-);
}
TreeNode* build(
vector<int>& preorder, int bp, int ep,
vector<int>& inorder, int bi, int ei
)
{
if ( bp>ep || bi>ei ) return NULL;
TreeNode* root = new TreeNode(preorder[bp]);
int pos = Solution::findPos(inorder, bi, ei, preorder[bp]);
int left_range = pos - bi;
root->left = Solution::build(preorder, bp+, bp+left_range, inorder, bi, bi+left_range-);
root->right = Solution::build(preorder, bp+left_range+, ep, inorder, bi+left_range+, ei);
return root;
}
int findPos(vector<int>& order, int begin, int end, int val)
{
for ( int i=begin; i<=end; ++i ) if (order[i]==val) return i;
}
};
【Construct Binary Tree from Preorder and Inorder Traversal】cpp的更多相关文章
- 【构建二叉树】01根据前序和中序序列构造二叉树【Construct Binary Tree from Preorder and Inorder Traversal】
我们都知道,已知前序和中序的序列是可以唯一确定一个二叉树的. 初始化时候二叉树为:================== 前序遍历序列, O================= 中序遍 ...
- 【题解二连发】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 - LeetCode Construct Binary ...
- 【LeetCode】105. Construct Binary Tree from Preorder and Inorder Traversal
Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...
- 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 ...
- Construct Binary Tree from Preorder and Inorder Traversal
Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...
- 36. Construct Binary Tree from Inorder and Postorder Traversal && Construct Binary Tree from Preorder and Inorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal OJ: https://oj.leetcode.com/problems/cons ...
- LeetCode: Construct Binary Tree from Preorder and Inorder Traversal 解题报告
Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...
- [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 ...
- [LeetCode] 105. 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 ...
随机推荐
- mysql触发器关联表更新
mysql> create table voteItem -> ( -> id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, -> titl ...
- [原]My first Python
我的第一个Python程序: print 'hello world' raw_input ("print any key to continue...") 在python3.4下应 ...
- MySQL连接语法
http://www.cnblogs.com/hanzhaoxin/p/3590642.html 内连接:INNER JOIN 内连接为 两个表中必须都同时满足条件 内连接,即最常见的等值连接自然连 ...
- MongoDB(1):常用操作命令大全
MongoDB常用操作命令大全(转) http://www.jb51.net/article/48217.htm 成功启动MongoDB后,再打开一个命令行窗口输入mongo,就可以进行数据库的一些操 ...
- WordPress 非插件实现拦截无中文留言
Some Chinese Please 插件可以拦截不带中文字的留言,之前本博客一直在用效果不错,不写入数据库,可有效地减少 spam 对服务器的无谓使用,其实可以将插件简化一下,直接用代码实现.将下 ...
- Sorl之.net操作
http://www.cnblogs.com/zhangweizhong/category/771055.html 插入: SolrNet.Startup.Init<Movie>(&quo ...
- SQL Server 基础:Join用法
Join(麻蛋 废话不多说 有图有真相) 测试数据脚本 CREATE TABLE Atable ( S# INT, Sname ), Sage INT, Sfrom ) ) insert into ...
- Form认证导致登陆页面的样式无效和图片不能显示的原因
最近在做企业内门户网站,一切进展还算顺利,部署到生产环境的时候也能没有什么大问题,只是登录页面的样式不起作用,不知为何,因为是使用了login控件,最初以为是此控件有内置默认样式或者什么原因,于是就不 ...
- python 上下文管理器
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 上下文管理器(context manager)是Python2.5开始支持的一种语 ...
- 使用supervisor的一些注意事项
一直都有在使用supervisor来管理linux上的服务进程.最近有同事说有某服务貌似有问题,让上去检查一下.上去以后发现某服务反应的确很慢,所以就用supervisor重启一下.但是重启的时候就发 ...