问题重述:

问题求解:

我们换一组比较有代表性的样例,

  1. 对于上图的树来说,
    index: 0 1 2 3 4 5 6
    先序遍历为: 2 4 5 3 6 7
    中序遍历为: 4 2 5 6 3 7
    为了清晰表示,我给节点上了颜色,红色是根节点,蓝色为左子树,绿色为右子树。
    提取期中根节点的左子树 2 4 5,可以把2 4 5看作新的index,由此:
    index 2 4 5
    先序遍历为:
    中序遍历为: 5
    同理,右子树也是如此。
    这样不难看出本题应该用递归方法解决。
  1. /**
  2. * Definition for a binary tree node.
  3. * public class TreeNode {
  4. * int val;
  5. * TreeNode left;
  6. * TreeNode right;
  7. * TreeNode(int x) { val = x; }
  8. * }
  9. */
  10. class Solution {
  11. public TreeNode buildTree(int[] preorder, int[] inorder) {
  12. TreeNode root = null;
  13. if(preorder.length == 0) return root;
  14. root = new TreeNode(preorder[0]);
  15. if(preorder.length == 1 && inorder.length == 1) {
  16. //root.val = preorder[0];
  17.  
  18. return root;
  19. }
  20. //root.val = preorder[0];
  21. int flag = 0;
  22. for(int i=0;i<inorder.length;i++){
  23. if(inorder[i] == preorder[0]) {
  24. flag = i;
  25. break;
  26. }
  27. }
  28. TreeNode lNode = null;
  29. TreeNode rNode = null;
  30. root.left = buildTree(Arrays.copyOfRange(preorder,1,flag+1),Arrays.copyOfRange(inorder,0,flag));
  31. root.right = buildTree(Arrays.copyOfRange(preorder,flag+1,preorder.length),Arrays.copyOfRange(inorder,flag+1,inorder.length));
  32. return root;
  33. }
  34. }
  1.  

LeetCodee 105. Construct Binary Tree from Preorder and Inorder Traversal的更多相关文章

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

  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 ----- java

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

  4. 105. Construct Binary Tree from Preorder and Inorder Traversal

    Given preorder and inorder traversal of a tree, construct the binary tree. ============== 基本功: 利用前序和 ...

  5. LeetCode OJ 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. 【leetocde】 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 ...

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

  8. 【一天一道LeetCode】#105. Construct Binary Tree from Preorder and Inorder Traversal

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源:http ...

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

随机推荐

  1. 00HTML

    一.概述 超文本标记语言(Hyper Text Markup Language),HTML是一门描述性的语言.基本语法: <标签> 内容 </标签>** 在一个网页中,HTML ...

  2. Codeforces Round #411 (Div. 2) C. Find Amir

    C. Find Amir time limit per test   1 second memory limit per test   256 megabytes   A few years ago ...

  3. 26_线程池_ThreadPoolExecutor

    [ThreadPoolExecutor简述] 无论是Executors类的newFixedThreadPool().newSingleThreadExecutor(),还是newCachedThrea ...

  4. 01_Nginx入门

    [Nginx概述] Nginx是一款轻量级的Web服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-Like协议下发行.其特点是占有内存少,并发能力强,事实上Nginx ...

  5. 安装busybox玩玩

    到http://www.busybox.net/downloads/binaries/下载放到sdcard然后adb shellsumount -o remount,rw -t yaffs2 /dev ...

  6. PHP 使用WampServer环境,如何配置虚拟主机域名

    很多人不会配置虚拟主机,我这里简单交一下大家,分三步: 1.在 C:\Windows\System32\drivers\etc 文件夹中的文件 Hosts 文件修改代码为: 127.0.0.1 loc ...

  7. Java中short、int、long、float、double的取值范围

    一.基本数据类型的特点,位数,最大值和最小值.1.基本类型:short 二进制位数:16 包装类:java.lang.Short 最小值:Short.MIN_VALUE=-32768 (-2的15此方 ...

  8. 安装skype for business server组件 报错“未满足先决条件”和安装KB2982006补丁提示“此更新不适用于你的计算机”

    安装skype for business server组件 报错“未满足先决条件” 上网经查询发现是没有安装KB2982006-x64 更新补丁 去官网上找这个补丁,发现这个补丁要热更新啥的,还要写邮 ...

  9. zimbra邮件服务器的搭建和迁移

    背景: 公司最近由于服务器费用问题,需要将邮件服务器从亚马逊(新加坡)云服务器A迁移到阿里云(香港)云服务器B. 由于邮箱使用的是域名访问,但是没有进行备案,所以只能迁移到港澳台地区,才能正常使用. ...

  10. QT的文件查找

    https://blog.csdn.net/hustyangju/article/details/17784007 http://www.cppblog.com/biao/archive/2011/1 ...