Given preorder and inorder traversal of a tree, construct the binary tree.

Note:
You may assume that duplicates do not exist in the tree.
从前序以及中序的结果中构造二叉树,这里保证了不会有两个相同的数字,用递归构造就比较方便了:

 class Solution {
public:
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
if(!preorder.size()) return NULL;
return createTree(preorder, , preorder.size() - , inorder, , inorder.size() - );//边界条件应该想清楚
} TreeNode* createTree(vector<int>& preOrder, int preBegin, int preEnd, vector<int>& inOrder, int inBegin, int inEnd)
{
if(preBegin > preEnd) return NULL;
int rootVal = preOrder[preBegin];
int mid;
for(int i = inBegin; i <= inEnd; ++i)
if(inOrder[i] == rootVal){
mid = i;
break;
}
int len = mid - inBegin; //左边区间的长度为mid - inBegin;
TreeNode * left = createTree(preOrder, preBegin + , preBegin + len, inOrder, inBegin, mid - );
TreeNode * right = createTree(preOrder, preBegin + len + , preEnd, inOrder, mid + , inEnd);
TreeNode * root = new TreeNode(rootVal);
root->left = left;
root->right = right;
return root;
}
};

java版本的代码如下所示,方法上与上面的没什么区别:

 public class Solution {
public TreeNode buildTree(int[] preorder, int[] inorder) {
return createTree(preorder, 0, preorder.length - 1, inorder, 0, inorder.length - 1);
} public TreeNode createTree(int[] preorder, int preBegin, int preEnd, int[] inorder, int inBegin, int inEnd){
if(preBegin > preEnd)
return null;
int rootVal = preorder[preBegin];
int i = inBegin;
for(; i <= inEnd; ++i){
if(inorder[i] == rootVal)
break;
}
int leftLen = i - inBegin;
TreeNode root = new TreeNode(rootVal);
root.left = createTree(preorder, preBegin + 1, preBegin + leftLen ,inorder, inBegin, i - 1); //这里的边界条件应该注意
root.right = createTree(preorder, preBegin + 1 + leftLen, preEnd, inorder, i + 1, inEnd);
return root;
}
}

LeetCode OJ:Construct Binary Tree from Preorder and Inorder Traversal(从前序以及中序遍历结果中构造二叉树)的更多相关文章

  1. LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal 由前序和中序遍历建立二叉树 C++

    Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...

  2. 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 t ...

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

  4. 【LeetCode】105. Construct Binary Tree from Preorder and Inorder Traversal 从前序与中序遍历序列构造二叉树(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...

  5. (二叉树 递归) 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 ...

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

  7. leetcode 105 Construct Binary Tree from Preorder and Inorder Traversal ----- java

    Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...

  8. Java for 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 ...

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

  10. Construct Binary Tree from Preorder and Inorder Traversal(根据前序中序构建二叉树)

    根据前序中序构建二叉树. 1 / \ 2 3 / \ / \ 4 5 6 7对于上图的树来说, index: 0 1 2 3 4 5 6 先序遍历为: 6 3 7为了清晰表示,我给节点上了颜色,红色是 ...

随机推荐

  1. SQL2008 R2直接恢复 mdf后缀数据文件

    数据库默认存储地址为   C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA  ,那么我首先把朋友给的,md ...

  2. 004-mysql explain详解

    一.使用 使用explain + 查询语句 二.解释说明 1)id列[执行顺序] id列数字越大越先执行,如果说数字一样大,那么就从上往下依次执行,id列为null的就表是这是一个结果集,不需要使用它 ...

  3. PyNest——part 2: populations of neurons

    part 2: populations of neurons introduction 在这篇讲义中,我们着眼于创建和参数化神经元批次,并将它们连接起来. 当你完成这些材料时,你会知道如何: 创建具有 ...

  4. Maven学习笔记—安装和配置

    Maven的安装和配置 1 在windows上安装maven 1.1 下载maven 访问maven的下载页面:http://maven.apache.org/download.cgi,选择版本下载即 ...

  5. Eclipse中执行maven命令(转载)

    转载自:http://blog.csdn.net/u011939453/article/details/43017865 1.如下图,右击需要执行maven命令的工程,选择"Debug As ...

  6. ALE和IDocs

    转自:http://blog.163.com/shenshengqge@126/blog/static/820512902011101152518635/ 作为目前ERP市场上最为领先的应用系统之一, ...

  7. PictureBox 双缓冲防止闪屏

    Bitmap bm = new Bitmap(this.pbTraffic.Image); BufferedGraphicsContext current = BufferedGraphicsMana ...

  8. PAT 天梯赛 L1-030. 一帮一 【STL】

    题目链接 https://www.patest.cn/contests/gplt/L1-030 思路 用三个 Vector 来分别存放 整个排名,以及男生的单独排名和女生的单独排名 从整个的排名 从上 ...

  9. iOS 认识runtime 中的三个指针 isa , IMP , SEL

    runtime中函数调用经常被提及的三个概念 isa,IMP,SEL 一  isa:是类指针,之所以说isa是指针是因为Class其实是一个指向objc_class结构体的指针,而isa 是它唯一的私 ...

  10. Linux 关机命令详解 转自脚本之家

    在linux下一些常用的关机/重启命令有shutdown.halt.reboot.及init,它们都可以达到重启系统的目的,但每个命令的内部工作过程是不同的. Linux centos重启命令: 1. ...