Question:

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

Note:
You may assume that duplicates do not exist in the tree.

Analysis:

给出一棵树的前序遍历和中序遍历,构建这棵二叉树。

注意: 你可以假设树中不存在重复的关键码。
 
思路:
前序遍历和中序遍历肯定是可以唯一确定一棵二叉树的。
 前序遍历的第一个节点肯定是根节点,得到根节点后再到中序遍历中,可以很容易的找出左子树和右子树,然后这样递归的找根节点,确立左子树、右子树……
 
Answer:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode buildTree(int[] preorder, int[] inorder) {
return buildTreeHelper(preorder, inorder, 0, preorder.length - 1, 0, inorder.length - 1);
} public TreeNode buildTreeHelper(int[] preorder, int[] inorder, int pre1, int pre2, int in1, int in2) {
if(pre1 > pre2)
return null;
int pivot = pre1;
int i = in1;
for(; i<in2; i++) {
if(preorder[pivot] == inorder[i])
break;
}
TreeNode t = new TreeNode(preorder[pivot]);
int len = i - in1;
t.left = buildTreeHelper(preorder, inorder, pivot+1, pivot+len, in1, i-1);
t.right = buildTreeHelper(preorder, inorder, pivot+len+1, pre2, i+1, in2);
return t;
} }

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

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

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

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

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

  4. Leetcode: Construct Binary Tree from Preorder and Inorder Transversal

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

  5. [leetcode]Construct Binary Tree from Preorder and Inorder Traversal @ Python

    原题地址:http://oj.leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/ 题意:根 ...

  6. [Leetcode] Construct binary tree from preorder and inorder travesal 利用前序和中续遍历构造二叉树

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

  7. LeetCode——Construct Binary Tree from Preorder and Inorder Traversal

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

  8. Leetcode: Construct Binary Tree from Preorder and Inorder Traversal, Construct Binary Tree from Inorder and Postorder Traversal

    总结: 1. 第 36 行代码, 最好是按照 len 来遍历, 而不是下标 代码: 前序中序 #include <iostream> #include <vector> usi ...

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

随机推荐

  1. python读写dbf数据库

    dbf数据库作为一种简单的数据库,曾经广泛使用.现在在金融领域还是有很多的应用之处,工作中遇到此类的问题,在此记录一下. 1. 读取dbf ''' 读取DBF文件 ''' def readDbfFil ...

  2. MySQL - Linux下安装

    本安装方式仅对5.7.21版本负责. 下载地址:wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.21-linux-glibc2 ...

  3. netty-socket.io点对点通讯和聊天室通讯

    netty-socketio是基于netty的socket.io服务实现,可以无缝对接前端使用的socketio-client.js. 相对于javaee的原生websocket支持(@serverE ...

  4. 【TP5.1】HTML标签自动转义,导致CKEditor保存内容无法正常显示!

    问题:使用Thinkphp5.1 开发的时候显示CKEditor保存的内容不符合预期. 希望的样子,肯定是不显示<p><b>等标签,而是下面的样子. 因为刚开始使用TP5.1和 ...

  5. PHP 代码规范、流程规范、git规范

    1. 命名规范 (1).变量命名规范 1.变量使用驼峰命名法 禁止使用拼音或者拼音加数字 2.变量也应具有描述性,杜绝一切拼音.或拼音英文混杂的命名方式 3.变量包数字.字母和下划线字符,不允许使用其 ...

  6. 实例讲解如何利用jQuery设置图片居中放大或者缩小

    大家有没有见过其他网站的图片只要鼠标放上去就能放大,移出去的时候就能缩小,而且一直保持居中显示!其实jQuery提供一个animate函数可以使图片放大和缩小,只要改变图片的长和高就OK啦!但是ani ...

  7. JZOJ 1738. Heatwave

    Description 给你N个点的无向连通图,图中有M条边,第j条边的长度为: d_j. 现在有 K个询问. 每个询问的格式是:A B,表示询问从A点走到B点的所有路径中,最长的边最小值是多少? I ...

  8. python系列3之内置函数和文件操作

    目录 自定义函数 内置函数 文件的操作 练习题 一. 自定义函数 1. 函数的创建 函数的创建 1.def关键字 2.函数名+() 3.冒号 4.缩进 5. return返回值,可以不写,默认的返回值 ...

  9. 前言 openwrt简介

    什么是openwrt?先看一下度娘怎么说. OpenWRT是一个高度模块化.高度自动化的嵌入式Linux系统,拥有强大的网络组件和扩展性,常常被用于工控设备.电话.小型机器人.智能家居.路由器以及VO ...

  10. c语言可变参数函数

    c语言支持可变参数函数.这里的可变指,函数的参数个数可变. 其原理是,一般情况下,函数参数传递时,其压栈顺序是从右向左,栈在虚拟内存中的增长方向是从上往下.所以,对于一个函数调用 func(int a ...