题目

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: 0 1 2 3 4 5 6
先序遍历为: 2 4 5 3 6 7
中序遍历为: 4 2 5 1 6 3 7
为了清晰表示,我给节点上了颜色,红色是根节点,蓝色为左子树,绿色为右子树。
可以发现的规律是:
1. 先序遍历的从左数第一个为整棵树的根节点。
2. 中序遍历中根节点是左子树右子树的分割点。
再看这个树的左子树:
先序遍历为:
中序遍历为: 4 5
依然可以套用上面发现的规律。 右子树:
先序遍历为:
中序遍历为: 6 7
也是可以套用上面的规律的。 所以这道题可以用递归的方法解决。
具体解决方法是:
通过先序遍历找到第一个点作为根节点,在中序遍历中找到根节点并记录index。
因为中序遍历中根节点左边为左子树,所以可以记录左子树的长度并在先序遍历中依据这个长度找到左子树的区间,用同样方法可以找到右子树的区间。
递归的建立好左子树和右子树就好。 代码如下:
 1     public TreeNode buildTree(int[] preorder, int[] inorder) {
 2          int preLength = preorder.length;
 3          int inLength = inorder.length;
 4         return buildTree(preorder, 0, preLength-1, inorder, 0, inLength-1);
 5      }
 6    
 7     public TreeNode buildTree(int[] pre, int preStart, int preEnd, int[] in, int inStart, int inEnd){
 8          if(inStart > inEnd || preStart > preEnd)
 9              return null;
              
         int rootVal = pre[preStart];
         int rootIndex = 0;
         for(int i = inStart; i <= inEnd; i++){
              if(in[i] == rootVal){
                  rootIndex = i;
                  break;
              }
          }
        
          int len = rootIndex - inStart;
          TreeNode root = new TreeNode(rootVal);
          root.left = buildTree(pre, preStart+1, preStart+len, in, inStart, rootIndex-1);
          root.right = buildTree(pre, preStart+len+1, preEnd, in, rootIndex+1, inEnd);
        
         return root;
      }
Reference:http://edwardliwashu.blogspot.com/2013/01/construct-binary-tree-from-preorder-and.html

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

  1. Construct Binary Tree from Preorder and Inorder Traversal [LeetCode]

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

  2. Construct Binary Tree from Preorder and Inorder Traversal——LeetCode

    Given preorder and inorder traversal of a tree, construct the binary tree. 题目大意:给定一个二叉树的前序和中序序列,构建出这 ...

  3. Construct Binary Tree from Preorder and Inorder Traversal

    Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...

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

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

  6. 【题解二连发】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 ...

  7. LeetCode: 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. [ 转载 ] Android JNI(一)——NDK与JNI基础

    Android JNI(一)——NDK与JNI基础 隔壁老李头 关注  4.4 2018.05.09 17:15* 字数 5481 阅读 11468评论 8喜欢 140 本系列文章如下: Androi ...

  2. BZOJ.1034.[ZJOI2008]泡泡堂(贪心)

    题目链接 容易想到田忌赛马.但是是不对的,比如2 3对1 3,按田忌赛马策略会3->1 2->3,但是3->3 2->1显然更优. 而如果按己方最强>=对方最强则开打,也 ...

  3. 概率dp总结

    终于做到概率dp题了,开个总结帖记录一下 首先是几篇论文:有关概率和期望问题的研究 做了这么多题,实际上没什么特别好总结的,就是搞清状态和转移,顺着写就行了,和基本dp差不多 概率是由过去到现在dp[ ...

  4. Atcoder Grand Contest 010 C - Cleaning 树贪心(伪)

    C - Cleaning 题目连接: http://agc010.contest.atcoder.jp/tasks/agc010_c Description There is a tree with ...

  5. ios数据保存

  6. 怎么让链式调用setTimeout停止

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. 使用Automation将Azure SQL DB容灾到本地

    前段时间有个客户有如下需求,将Azure SQL Database数据库容灾到本地(备份频率1小时/次). 那么对于这样的需求,我们可以使用Azure Automation来实现自动化备份到本地. 大 ...

  8. 使用 IntraWeb (24) - 基本控件之 TIWFileUploader、TIWFile

    TIWFileUploader 是基于 Ajax 的上传控件, 最初是 Andrew Valums 开发, 从 IntraWeb XIV 纳入并替换 TIWFile. 虽然从组件面板上还能看到 TIW ...

  9. FireDAC 下的 Sqlite [11] - 关于批量提交 SQL 命令的测试

    可把下面代码直接贴在空白窗体上, 以快速完成窗体设计: object DBGrid1: TDBGrid Left = 0 Top = 0 Width = 265 Height = 338 Align ...

  10. Open JTAG Project

    Open JTAG Project is an open source hardware and software for a high speed USB JTAG tool. In this si ...