题目:

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的更多相关文章

  1. 【构建二叉树】01根据前序和中序序列构造二叉树【Construct Binary Tree from Preorder and Inorder Traversal】

    我们都知道,已知前序和中序的序列是可以唯一确定一个二叉树的. 初始化时候二叉树为:================== 前序遍历序列,           O================= 中序遍 ...

  2. 【题解二连发】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 ...

  3. 【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 ...

  4. 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 ...

  5. Construct Binary Tree from Preorder and Inorder Traversal

    Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...

  6. 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 ...

  7. LeetCode: Construct Binary Tree from Preorder and Inorder Traversal 解题报告

    Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...

  8. [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 ...

  9. [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 ...

随机推荐

  1. AWS学习之EC2

    1.AWS简介 AWS(Amazon Web Services )提供了一整套云计算服务,让您能够构建复杂.可扩展的应用程序.如今,成千上万各种规模的客户都在使用这些云计算服务,它们涉及各个行业,包括 ...

  2. LVS+keepalived实现负载均衡

    背景:         随着你的网站业务量的增长你网站的服务器压力越来越大?需要负载均衡方案!商业的硬件如F5又太贵,你们又是创业型互联公司如何有效节约成本,节省不必要 的浪费?同时实现商业硬件一样的 ...

  3. 16)JAVA实现回调(Android,Swing中各类listener的实现)

           熟悉MS-Windows和X Windows事件驱动设计模式的开发人员,通常是把一个方法的指针传递给事件源,当某一事件发生时来调用这个方法(也称为"回调").Java ...

  4. Adapter的getView方法详解

    来自:http://blog.csdn.net/yelbosh/article/details/7831812 BaseAdapter就Android应用程序中经常用到的基础数据适配器,它的主要用途是 ...

  5. 十款基础级WordPress插件

    1.Akismet插件 Akismet是全球最受欢迎的反垃圾插件,专为对抗"博客spam"."评论spam"而生.Akismet是WordPress官方插件之一 ...

  6. android 模拟按键事件

    模拟按键事件可以提高代码的复用性,比如在一个edittext的回车事件里做的一些处理 在该edittext的另一个输入要做相同的处理时,模拟按键事件就非常方便了. 代码很简单,直接上代码: new T ...

  7. Java 为什么使用抽象类和接口

    Java接口和Java抽象类代表的就是抽象类型,就是我们需要提出的抽象层的具体表现.OOP面向对象的编程,如果要提高程序的复用率,增加程序的可维护性,可扩展性,就必须是面向接口的编程,面向抽象的编程, ...

  8. Python开发【第一篇】Python基础之字符串格式化

    字符串格式化 Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存.[PEP-310 ...

  9. JLINK V8 Keil MDK4.10 STM32

    新买的JLINK v8仿真器,第一次使用,编译环境是Keil MDK4.10,目前芯片是STM32F103x. 按照光盘的说明先安装了驱动,USB接上JLINK v8,显示驱动成功.但是在debug或 ...

  10. WPF数据双向绑定

    设置双向绑定,首先控件要绑定的对象要先继承一个接口: INotifyPropertyChanged 然后对应被绑定的属性增加代码如下: 意思就是当Age这个属性变化时,要通知监听它变化的人. 即:Pr ...