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

题目大意:给定一个二叉树的前序和中序序列,构建出这个二叉树。

解题思路:跟中序和后序一样,在先序中取出根节点,然后在中序中找到根节点,划分左右子树,递归构建这棵树,退出条件就是左右子树长度为1,则返回这个节点,长度为0则返回null。

Talk is cheap:

    public TreeNode buildTree(int[] preorder, int[] inorder) {
if (preorder == null || inorder == null || inorder.length == 0 || preorder.length == 0) {
return null;
}
int preLen = preorder.length;
int inLen = inorder.length;
TreeNode root = new TreeNode(preorder[0]);
if (preLen == 1) {
return root;
}
int pos = 0;
for (int i = 0; i < inLen; i++) {
if (inorder[i] == preorder[0]) {
pos = i;
break;
}
} int[] preLeft = Arrays.copyOfRange(preorder, 1, pos + 1);
int[] inLeft = Arrays.copyOfRange(inorder, 0, pos);
int[] preRight = Arrays.copyOfRange(preorder, pos + 1, preLen);
int[] inRight = Arrays.copyOfRange(inorder, pos + 1, inLen);
root.left = buildTree(preLeft, inLeft);
root.right = buildTree(preRight, inRight);
return root;
}

Construct Binary Tree from Preorder and Inorder Traversal——LeetCode的更多相关文章

  1. Construct Binary Tree from Preorder and Inorder Traversal [LeetCode]

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

  2. Construct Binary Tree from Preorder and Inorder Traversal leetcode java

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

  3. Construct Binary Tree from Preorder and Inorder Traversal

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

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

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

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

  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】105. Construct Binary Tree from Preorder and Inorder Traversal

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

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

随机推荐

  1. css 实现页面加载中等待效果

    <!DOCTYPE html> <html> <head> <title>css实现页面加载中,请稍候效果</title> <meta ...

  2. OSI 网络七层模型(笔记)

    一直以来我们都在使用着互联网,每天聊着qq,上着淘宝,但是却不了解怎么运行的呢,充满了好奇.今天同过了解来总结一下OSI网络七层模型: 上一张图 OSI (open system interconne ...

  3. [原创] SQLite数据库使用清单(上)

    1. 介绍 1.1 安装 访问 SQLite 下载页面,从 Windows 区下载预编译的二进制文件. 您需要下载 sqlite-shell-win32-*.zip 和 sqlite-dll-win3 ...

  4. 在使用Kettle的集群排序中 Carte的设定——(基于Windows)

    本片文章主要是关于使用Kettle的UI界面: Spoon来实现基于集群的对数据库中的数据表数据进行排序的试验. 以及在实验过程中所要开启的Carte服务的一些配置文件的设置, 还有基于Windows ...

  5. ASP.NET Excel数据导出数据库

    /// <summary> /// 根據gridview導出excel /// </summary> /// <param name="ctl"> ...

  6. Missing artifact com.sun:tools:jar:1.5.0

    http://java-suddy.iteye.com/blog/1821871(解决了我的问题)

  7. ASP.NET 导入EXCEL文档

    鉴于教务一般都是手动输入学生信息,在未了解本校数据库的客观情况之下,我们准备设计一个导入excel文档中学生信息如数据库的功能.结合网上各类大牛的综合版本出炉.. 首先具体的实现思想如下: 1.先使用 ...

  8. [转]Windows中的命令行提示符里的Start命令执行路径包含空格时的问题

    转自:http://www.x2009.net/articles/windows-command-line-prompt-start-path-space.html 当使用Windows 中的命令行提 ...

  9. What is SaaS?

    SaaS, or Software as a Service, describes any cloud service where consumers are able to access softw ...

  10. iOS 数据持久性存储-对象归档

    对象归档是将对象归档以文件的形式保存到磁盘中(也称为序列化,持久化),使用的时候读取该文件的保存路径读取文件的内容(也称为解档,反序列化) 主要涉及两个类:NSKeyedArichiver.NSKey ...