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

题目大意:给定一个二叉树的前序和中序序列,构建出这个二叉树。

解题思路:跟中序和后序一样,在先序中取出根节点,然后在中序中找到根节点,划分左右子树,递归构建这棵树,退出条件就是左右子树长度为1,则返回这个节点,长度为0则返回null。

Talk is cheap:

    public TreeNode buildTree(int[] preorder, int[] inorder) {
if (preorder == null || inorder == null || inorder.length == 0 || preorder.length == 0) {
return null;
}
int preLen = preorder.length;
int inLen = inorder.length;
TreeNode root = new TreeNode(preorder[0]);
if (preLen == 1) {
return root;
}
int pos = 0;
for (int i = 0; i < inLen; i++) {
if (inorder[i] == preorder[0]) {
pos = i;
break;
}
} int[] preLeft = Arrays.copyOfRange(preorder, 1, pos + 1);
int[] inLeft = Arrays.copyOfRange(inorder, 0, pos);
int[] preRight = Arrays.copyOfRange(preorder, pos + 1, preLen);
int[] inRight = Arrays.copyOfRange(inorder, pos + 1, inLen);
root.left = buildTree(preLeft, inLeft);
root.right = buildTree(preRight, inRight);
return root;
}

Construct Binary Tree from Preorder and Inorder Traversal——LeetCode的更多相关文章

  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 java

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

  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. poj 1849 Two

    /*poj 1849 two 思考一下会发现 就是求直径 直径上的中点就是两个人分开的地方(不再有交集)*/ #include<cstdio> #define maxn 100010 us ...

  2. 使用js使表单自动提交

    function sub(){ document.yeepay.submit(); } setTimeout(sub,1000);//以毫秒为单位的.1000代表一秒钟.根据你需要修改这个时间. // ...

  3. (转)system()函数

    [C/C++]Linux下system()函数引发的错误   今天,一个运行了近一年的程序突然挂掉了,问题定位到是system()函数出的问题,关于该函数的简单使用在我上篇文章做过介绍: http:/ ...

  4. C# Chart圖標綁定

    开发软件为VS2010 免去了安装插件之类的麻烦. 最终效果图: 饼状图: 前台设置:设置参数为: :Titles, 添加一个序列,在Text中设置名字. :Series ,添加一个序列,选择Char ...

  5. 使用ng-if,获取不到里面的ng-model值,解决方案

    当使用ng-if时,是会把默认作用域删除的,当其为true时,只是增加了其界面元素,为最原始状态,控制器在其上是不起作用的,要想获取ng-if中的值,可以用$scope.$$childTail.lay ...

  6. 1、大部分社交平台接口不支持https协议。

    参考文献来自:http://wiki.mob.com/ios9-%E5%AF%B9sharesdk%E7%9A%84%E5%BD%B1%E5%93%8D%EF%BC%88%E9%80%82%E9%85 ...

  7. 段落排版--行间距, 行高(line-height)

    <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...

  8. JavaScript HTML DOM EventListener

    JavaScript HTML DOM EventListener addEventListener() 方法 实例 点用户点击按钮时触发监听事件: document.getElementById(& ...

  9. 【模板】【网络流】Dinic

    /* 唐代杜荀鹤 <小松> 自小刺头深草里,而今渐觉出蓬蒿. 时人不识凌云木,直待凌云始道高. */ #include <iostream> #include <cstd ...

  10. 调用数据库过程函数mysql

    Connection conn=JdbcUtil.getConnection();//JdbcUtil是我写的获取connection的工具类 CallableStatement cast=conn. ...