【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 ...
随机推荐
- oracle 数组类型
create or replace function my_test(p_str varchar2) return number as --普通变量 v_var ); --固定长度数组 type v_ ...
- Bootstrap Alert Auto Close
http://stackoverflow.com/questions/23101966/bootstrap-alert-auto-close http://jsfiddle.net/mfX57/ $( ...
- ajax+php+mysql更新
html代码 <input type="button" id="quxiao" class="quxiao" name="q ...
- mssql 下删除 default 值的Sql写法
FROM Sys.default_constraints a JOIN sys.columns b ON a.parent_object_id = b.object_id AND a.parent_c ...
- MVC MVVM Knockout 常遇问题总结
1.模板绑定(使用插件jquery.tmpl) var ViewModel={Product:ko.observable()} <div data-bind="template:{na ...
- zoj 2334 Monkey King/左偏树+并查集
原题链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1389 大致题意:N只相互不认识的猴子(每只猴子有一个战斗力值) 两只 ...
- golang与C交互:cgo
1. 在Go中引用C代码很简单, 在 import "C"前用注释引入标准的C代码, 然后使用C.xxx的伪包引用C代码空间的标识符即可. 需要注意, import"C& ...
- python中的小技巧
1.求1~100以内的素数 prime=filter(lambda x: not [x%i for i in range(2,x) if x%i==0], range(2,101))#列表推导,一行搞 ...
- iOS高级编程之XML,JSON数据解析
解析的基本概念 所谓“解析”:从事先规定好的格式串中提取数据 解析的前提:提前约定好格式.数据提供方按照格式提供数据.数据获取方按照格式获取数据 iOS开发常见的解析:XML解析.JSON解析 一.X ...
- WordPress实现长篇文章/日志/单页面分页功能效果
在WordPress里写文章,如果内容很多,你可能想要把文章分成几页来让访客浏览,这样既保持了网页的美观,也提高了网页的打开速度.但是在WordPress默认提供的按钮里,你可能找不到文章分页功能所对 ...