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. java枚举类

    enum关键字用于定义枚举类,若枚举只有一个成员, 则可以作为一种单例模式的实现方式.   枚举类对象的属性不应允许被改动, 所以应该使用 private final 修饰. 枚举类的使用 priva ...

  2. ODI中显示us7ascii字符集的测试

    安装oracle DB时,选择的字符集:美国.英语.US7ASCII. 在不设置nls_lang的情况,插入中文,成功,但存进去的是乱码,select看到也是??(无论后面再怎么设置nls_lang) ...

  3. Javascript 基础--数组

    一.一维数组 1.一维数组 var weights = [3,5,1,3.4,2,50]; var all_weight=0; var avg_weight=0; for(var i=0;i<w ...

  4. Asp.Net应用运行原理

    一.运行原理图 二.对于HttpModule和HttpHandler的概念可能还不是很清楚,请先看Asp.Net应用生命周期.RAR 或者 Asp.Net深入解析 第四章,流程图太大无法粘贴 三.传智 ...

  5. php练习题:投票

    通过连接数据库,对数据库的增删改来实现一个投票的进行与结果的显示: 方法一: 主页面 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Tran ...

  6. UNIX 网络编程第三版

    第五章p102: ps -t  pts/6 -o pid,ppid,tty,stat,args,wchan 在我的系统上运行时出现:TTY not found linux发行版为mint17.1 改用 ...

  7. The 1st day with Python

    刚开始实践python,遇到比较多的问题就是函数名.变量名输入错误,比较给力的按无论shell还是terminal给出的错误提示,按图索骥都能在网上找到相关解决办法,简单的自己也能顿悟. 典型的一个是 ...

  8. ELF Spec

    ELF Spec Generic System V Application Binary Interface,ELF-64 Object File Format AMD64 System V ABI, ...

  9. BZOJ 3997 组合数学

    好厉害. 注意到到了(i,j)就一定到不了(i-1,j+1),那么可以dp啦.dp[i][j]表示(i,j)右上角都清了的方案数. #include<iostream> #include& ...

  10. Makefile总结和反序字符数组,整体右移数组,杨辉三角!

    2015.1.23今天我值日 继续接着昨天Makefile的目标开始记录:第一种:有 .PHNOY声明,但是冒号后面没有依赖文件.PHNOY:clean clean://没有依赖文件 rm *.0 t ...