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

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

给出前序遍历和中序遍历,然后求这棵树。

很有规律。递归就可以实现。

/**
* 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) { int len = preorder.length;
if( len == 0)
return null;
return helper(preorder,0,len-1,inorder,0,len-1); } public TreeNode helper( int[] preorder,int pre_start,int pre_end,int[] inorder,int in_start,int in_end){ if( pre_start > pre_end || in_start > in_end )
return null;
TreeNode node = new TreeNode(preorder[pre_start]); int size = 0;
for( int i = in_start;i<=in_end && inorder[i] != preorder[pre_start];i++,size++)
;
node.left = helper(preorder,pre_start+1,pre_start+size,inorder,in_start,in_start+size-1); node.right = helper(preorder,pre_start+size+1,pre_end,inorder,in_start+size+1,in_end); return node; }
}

速度不算快,速度最快的答案也进行了参考。

并不是算法有多好,只是他在

for( int i = in_start;i<=in_end && inorder[i] != preorder[pre_start];i++,size++)
;

这里遍历的时候选择了从后向前遍历,由于测试数据的特殊性,导致了其答案的快速性。

leetcode 105 Construct Binary Tree from Preorder and Inorder Traversal ----- java的更多相关文章

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

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

  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 由前序和中序遍历建立二叉树 C++

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

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

  6. [leetcode] 105. Construct Binary Tree from Preorder and Inorder Traversal (Medium)

    原题 题意: 根据先序和中序得到二叉树(假设无重复数字) 思路: 先手写一次转换过程,得到思路. 即从先序中遍历每个元素,(创建一个全局索引,指向当前遍历到的元素)在中序中找到该元素作为当前的root ...

  7. Leetcode#105 Construct Binary Tree from Preorder and Inorder Traversal

    原题地址 基本二叉树操作. O[       ][              ] [       ]O[              ] 代码: TreeNode *restore(vector< ...

  8. leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal,剑指offer 6 重建二叉树

    不用迭代器的代码 class Solution { public: TreeNode* reConstructBinaryTree(vector<int> pre,vector<in ...

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

随机推荐

  1. IT公司100题-12-求1+2+…+n

    问题描述: 求1+2+…+n,要求不能使用乘除法.for.while.if.else.switch.case等关键字以及条件判断语句(A?B:C).   分析: 利用类的静态变量实现: new一含有n ...

  2. 【C语言学习】-02 分支结构

    本文目录: 一.BOOL布尔类型 二.关系运算符 三.逻辑运算符 四.if语句 五.枚举类型 六.switch语句 一.BOOL布尔类型 BOOL数据类型,是一种表示非真即假的数据类型,布尔类型的变量 ...

  3. 用K2 smartforms开发一个应用程序究竟比ASP.NET快多少?

    这次试验的起因是一场内部辩论. “用K2 smartforms开发一个应用程序究竟比ASP.NET快多少?” 我们推测是快4倍. 但是经过测试发现,我们推测错了. 本文记录了试验的规划.过程以及令人惊 ...

  4. 3D中的切线空间简介

    转自:http://www.cnblogs.com/cxrs/archive/2009/10/25/1589515.html 1. 什么是Tangent space? Tangent space和wo ...

  5. Ubuntu 14.10 下网络流量实时监控ifstat iftop命令详解

    ifstat 介绍 ifstat工具是个网络接口监测工具,比较简单看网络流量 实例 默认使用 #ifstat eth0 eth1 KB/s in KB/s out KB/s in KB/s out 0 ...

  6. META标签的NAME变量

    META标签的NAME变量语法格式是: <META NAME=xxx CONTENT=xxxxxxxxxxxxxxxxxx> 其中xxx主要有下面几种参数: 1. Keywords(关键字 ...

  7. java多线程的协作

    java多线程之间相互协作,主要有join,  yield,  interupt(),  sleep,  wait,  notify,  notifyAll; join: 在一个线程A的代码里面调用另 ...

  8. What is hmux in resin?

    When I start the Resin server it says hmux listening to localhost:6802 What is this hmux? Is this a ...

  9. Makefile学习笔记

    ls -l 查看文件详细信息 1.gcc -E test.c -o test.i//预编译gedit test.i //查看:高级C 2.gcc -Wall -S test.i -o test.s// ...

  10. vijos 1776 关押罪犯

    带权并查集+贪心. #include<iostream> #include<cstdio> #include<cstring> #include<algori ...