题目:

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

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

解题思路分析:

前序遍历首先遍历根节点,然后依次遍历左右子树

中序遍历首先遍历左子树,然后遍历根结点,最后遍历右子树

根据二者的特点,前序遍历的首节点就是根结点,然后在中序序列中找出该结点位置(index),则该结点之前的为左子树结点,该节点之后为右子树结点;

同样对于前序序列,首结点之后的index个结点为左子树结点,剩余的节点为右子树结点;

最后,递归建立子树即可。

java代码:

 public class Solution {
public TreeNode buildTree(int[] preorder, int[] inorder) {
if(preorder == null || preorder.length == 0){
return null;
} int flag = preorder[0];
TreeNode root = new TreeNode(flag);
int i = 0;
for(i=0; i<inorder.length; i++){
if(flag == inorder[i]){
break;
}
} int[] pre_left, pre_right, in_left, in_right;
pre_left = new int[i];
for(int j=1; j<=i;j++){
pre_left[j-1] = preorder[j];
}
pre_right = new int[preorder.length-i-1];
for(int j=i+1; j<preorder.length;j++){
pre_right[j-i-1] = preorder[j];
} in_left = new int[i];
for(int j=0;j<i;j++){
in_left[j] = inorder[j];
}
in_right = new int[inorder.length-i-1];
for(int j=i+1; j<inorder.length; j++){
in_right[j-i-1] = inorder[j];
}
root.left = buildTree(pre_left,in_left);
root.right = buildTree(pre_right,in_right); return root; } }

leetcode105:Construct Binary Tree from Preorder and Inorder Traversal的更多相关文章

  1. LeetCode OJ: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 ...

  2. 【LeetCode105】Construct Binary Tree from Preorder and Inorder Traversal★★

    1.题目 2.思路 3.java代码 //测试 public class BuildTreeUsingInorderAndPreorder { public static void main(Stri ...

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

  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 Inorder and Postorder Traversal & Construct Binary Tree from Preorder and Inorder Traversal

    LeetCode 原题链接 Construct Binary Tree from Inorder and Postorder Traversal - LeetCode Construct Binary ...

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

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

  7. 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. 5分钟 wamp下php phpmaile发送qq邮件 2015最新方法说明

    13:40 2015/11/20 5分钟 wamp下php phpmaile发送qq邮件 2015最新方法说明 关键点:现在qq邮箱开通smtp服务后会给你一个很长的独立新密码,发邮件配置中的密码需要 ...

  2. JavaScript进阶(三)之对象

    返回星期方法 getDay() 返回星期,返回的是0-6的数字,0 表示星期天.如果要返回相对应“星期”,通过数组完成,代码如下: <script type="text/javascr ...

  3. CF 335B. Palindrome(DP)

    题目链接 挺好玩的一个题,1Y... #include <cstdio> #include <cstring> #include <iostream> using ...

  4. linux修改系统编码

    Windows的默认编码为GBK,Linux的默认编码为UTF-8.在Windows下编辑的中文,在Linux下显示为乱码.一种方法是在windows进行转码,比如使用ue工具在文件-->转换 ...

  5. MyBatis的几种批量操作

    MyBatis中批量插入 方法一: <insert id="insertbatch" parameterType="java.util.List"> ...

  6. Oracle 建立索引及SQL优化

    数据库索引: 索引有单列索引,复合索引之说,如果某表的某个字段有主键约束和唯一性约束,则Oracle 则会自动在相应的约束列上建议唯一索引.数据库索引主要进行提高访问速度. 建设原则: 1.索引应该经 ...

  7. 360safe安全卫士防网站攻击源码

    近段时间,公司网站老被攻击,于是研究起防止攻击方法,当然无外乎就是SQL注入之类的问题,无意间发现了一个360安全卫士提供的源码,觉得挺好的,咋们暂且不说防攻击效果,至少思路是很好的,奉献给大家,大家 ...

  8. hdu1240 bfs 水题

    原题链接 思路:水题,直接搜 #include "map" #include "queue" #include "math.h" #incl ...

  9. [CareerCup] 18.7 Longest Word 最长的单词

    5.7 Given a list of words, write a program to find the longest word made of other words in the list. ...

  10. ubuntu上安装Eclipse时遇到的一个错误

    A Java Runtime Environment (JRE) or Java Development Kit (JDK)must be available in order to run Ecli ...