题目:

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. lsof在运维中的应用

    场景一:文件系统使用率很高,但是找不到具体哪个文件占用了空间 原因:在unix系统中,如果有两个进程同时使用一个文件,如果其中一个进程删除了这个文件,但是这个文件此刻不会正真被释放,一直要等待引用它的 ...

  2. php+curl上传文件

    因为公司项目用java做的,需要我这边用php上传文件.只给了个接口,参数都不明确,然后这边不提交表单,在生成pdf之后就立马上传.用了php+curl,总是没上传成功,这里看到了篇文章http:// ...

  3. 表单的enctype property

    enctype 属性规定在发送到服务器之前应该如何对表单数据进行编码. 默认地,表单数据会编码为 "application/x-www-form-urlencoded".就是说,在 ...

  4. 括号配对nyoj2(疑问)

    描述现在,有一行括号序列,请你检查这行括号是否配对.   输入 第一行输入一个数N(0<N<=100),表示有N组测试数据.后面的N行输入多组输入数据,每组输入数据都是一个字符串S(S的长 ...

  5. vi/vim初步接触

    vi和vim一直被人津津乐道,到底是什么使得它们如此受欢迎? vi分为3种模式:一般模式,编辑模式,命令行模式. (1)一般模式: 进入vi后,默认就是一般模式. 用处:方便地移动光标,删除字符/整行 ...

  6. IOS第18天(6,CAKeyframeAnimation关键帧动画)

    ******* #import "HMViewController.h" @interface HMViewController () @property (weak, nonat ...

  7. 树莓派文档翻译 - 使用 - GPIO: 树莓派A和B

    https://www.raspberrypi.org/documentation/usage/gpio/README.md 2016/6/25 GPIO: 树莓派A和B ##介绍GPIO和在树莓派上 ...

  8. ThinkPHP 3.2.3 简单后台模块开发(一)常用配置

    一.项目分组 下载解压 ThinkPHP 3.2.3,在默认的应用 Application(./Application) 中,包含一个默认的模块 Home(./Application/Home). 需 ...

  9. [转]Haroopad Markdown 编辑器代码语法高亮支持

    代码语法高亮 书写格式为: ` ` ` language_key if (condition){ return true } ` ` ` 在 ` ` ` (三个反引号)之间的是代码,其中languag ...

  10. 8 ways rich people view the world differently than the average person

    Self-made millionaire Steve Siebold spent 26 years interviewing some of the wealthiest people in the ...