【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 ...
随机推荐
- RegisterStartupScript和RegisterClientScriptBlock的区别
1. //注册到 <form> 尾部 ,HTML元素已加载完毕 this.Page.ClientScript.RegisterStartupScript(this.GetType( ...
- 通过jquery 获取文本框的聚焦和失焦方法
我还是喜欢用jquery来实现,不管页面中多少个输入框需要实现聚焦,失焦,都公有,我常用的方法是: 遍历该页面中的input框,获取输入框中的val值,当该输入框聚焦的时候跟存放的oldValue值进 ...
- C# .Net三层架构[转]
C# .Net三层架构[转] 编写人:CC阿爸 2014-3-14 希望朋友们留下自己对三层架构的理解... 三层体系结构的概念 用户界面表示层(USL) 业务逻辑层(BLL) 数据访问层(D ...
- HBase从hdfs导入数据
需求:将HDFS上的文件中的数据导入到hbase中 实现上面的需求也有两种办法,一种是自定义mr,一种是使用hbase提供好的import工具 一.hdfs中的数据是这样的 每一行的数据是这样的id ...
- android 分段显示文本颜色控件
效果: 使用: <com.bei.myapplication.app.ProgressTextView xmlns:ptv="http://schemas.android.com/ap ...
- CDH 不能监控hadoop状态
1 背景:公司集群整体搬迁 2 问题:hadoop重启后,发现一个非常坑爹的问题.用 hadoop dfsadmin -report 查看datanode状态完全正常,但是Cloudera Manag ...
- 利用java读写Excel文件
一.读取Excel文件内容 java 代码 public static String readExcel(File file){ StringBuffer sb = new StringBuffer( ...
- System.Transaction (TransactionScope) 与 可提升 (Promotable) 交易
这是我的备份,原文请看 http://www.dotblogs.com.tw/mis2000lab/archive/2014/11/12/transactionscope_promotable_tr ...
- 19.python的编码问题
在正式说明之前,先给大家一个参考资料:戳这里 文章的内容参考了这篇资料,并加以总结,为了避免我总结的不够完善,或者说出现什么错误的地方,有疑问的地方大家可以看看上面那篇文章. 以下说明是针对于pyth ...
- openerp 经典收藏 记录规则 – 销售只能看到自己的客户,经理可以看到全部(转载)
记录规则 – 销售只能看到自己的客户,经理可以看到全部 原文地址:http://cn.openerp.cn/record_rule/ OpenERP中的权限管理有四个层次: 菜单级别: 即,不属于指定 ...