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. 近期C++编译问题汇总

    编译c++ 代码中遇到几个问题,汇总一下: 1.编译openssl 遇到问题如图 ,  原因:不支持汇编编译,在perl编译指令中加入: no-asm , 如:perl Configure VC-WI ...

  2. [vijos P1112] 小胖的奇偶

    第一次看到这题怎么也不会想到是并查集题目…星期五第一次看到这题,到今天做出来,实在是废了好多功夫.看了很多人的解题都有same和diff数组,我也写了,后来发现不对啊两个数组的话find函数怎么写呢? ...

  3. SharePoint加K2,将Portal系统与BPM系统完美整合!

    K2 blackPearl与Microsoft Office SharePoint 一起为解决人员和流程相互合作的解决方案而提供一个强大的平台. K2“blackpearl”根据企业的需求提供了设计, ...

  4. C# Async/Await异步函数原理

    原理 与同步函数相比,CLR在执行异步函数时有几个不同的特点: 1.        并非一次完成,而且分多次完成 2.        并非由同一个线程完成,而是线程池每次动态分配一个线程来处理: 结合 ...

  5. 使用JsonObject解析json

    第一种: [ { "0": "1", "1": "一", "id": "1", ...

  6. UVa 10318 Security Panel

    题意:给你一个3*3的翻转模版,深色部分表示翻转,浅色部分不变.然后你可以在r*c的矩形里依照模版进行翻转,要求所有点亮所有块.输出最小的步骤. 思路:有一点比较好想.每个块至多被翻转一次,翻两次的效 ...

  7. 【LeetCode】Rotate Array

    Rotate Array Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = ...

  8. win7 64位搭建scrapy

    http://blog.csdn.net/it_yuan/article/details/23856713 win7 64位系统依赖的scrapy文件链接:http://pan.baidu.com/s ...

  9. Windows共享设定-使用net use添加网络盘带上账号密码

    食欲 net use \\10.11.1.2\ipc$ /user:dmnm\usr "pwd"

  10. linux远程连接mysql数据库

    在linux系统中,远程连接mysql数据库时不仅需要开启ip访问,还需要将防火墙中添加相应规则 在wdlinux中的[安全管理]菜单下选择[防火墙[iptables]]选项,增加端口为3306的规则 ...