题目链接:https://leetcode-cn.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/

参考链接:https://blog.csdn.net/qq_36172443/article/details/81105258

     https://www.lintcode.com/problem/clone-binary-tree/description

     https://www.cnblogs.com/phdeblog/p/9309219.html

首先你得理解如何递归建立一颗二叉树,然后才能理解本题。网上有很多解法都是通过变化中序遍历和前序续遍历的index来求解本题,这样的解法很容易弄错。如何变化index这是一个难点,该解法通过数组copy避免了计算index。

    public TreeNode buildTree(int[] pre, int[] in) {
if (pre.length == 0) {
return null;
}
int i = 0;
for (; i < in.length; i++) {
if (pre[0] == in[i]) {
break;
}
}
TreeNode node = new TreeNode(pre[0]);
//[1,i] [0,i-1]
//[i+1,length-1] [i+1,length-1]
node.left = buildTree(
Arrays.copyOfRange(pre, 1, i + 1),
Arrays.copyOfRange(in, 0, i));
node.right = buildTree(
Arrays.copyOfRange(pre, i + 1, pre.length),
Arrays.copyOfRange(in, i + 1, in.length));
return node; }

  

树之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 (用先序和中序树遍历来建立二叉树)

    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. Note:You may assume ...

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

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

  7. 105. Construct Binary Tree from Preorder and Inorder Traversal (Tree; DFS)

    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 从前序与中序遍历序列构造二叉树(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...

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

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

随机推荐

  1. cocos2d JS-(JavaScript) cc.each循环遍历对象

    有了它,妈妈再也不用担心我的数组会越界啦!! each()方法能使DOM循环结构简洁,不容易出错.each()函数封装了十分强大的遍历功能,使用也很方便,它可以遍历一维数组.多维数组.DOM, JSO ...

  2. 地图服务报 error #2035

    参考:https://blog.csdn.net/iteye_20296/article/details/82395628 现在问题解决了,确实是config.xml里关于这个widget的配置url ...

  3. 《linux就该这么学》找到一本不错的Linux电子书,《Linux就该这么学》。

    本帖不是广告贴,只是感觉有好的工具书而已 本书是由全国多名红帽架构师(RHCA)基于最新Linux系统共同编写的高质量Linux技术自学教程,极其适合用于Linux技术入门教程或讲课辅助教材,目前是国 ...

  4. 配置hdfs之后发现9000端口未被监听[玄学]

    1. 按照apache的官网的文档配置hdfs 2. 在 core-site.xml 中配置了 fs.defaultFS 的值为 hdfs://0.0.0.0:9000 3. 执行 start-dfs ...

  5. python中的lxml模块

    Python中自带了XML的模块,但是性能不太好,相比之下,LXML增加了很多实用的功能. lxml中主要有两部分, 1) etree,主要可以用来解析XML字符串, 内部有两个对象,etree._E ...

  6. 1.23 codeforces div3 C.Nice Garland

    You have a garland consisting of nn lamps. Each lamp is colored red, green or blue. The color of the ...

  7. SVN windows内修改日志内容(错误解决)

    在我的电脑是windows 7,使用TortoiseSVN客户端,选中代码目录,点击右键,选择<显示日志> 显示日志信息 修改原来的日志信息(在需要修改的版本的日志中点击鼠标右键,显示如下 ...

  8. Spring tokenizeToStringArray

    tokenizeToStringArray: StringUtils.tokenizeToStringArray(pattern, this.pathSeparator, this.trimToken ...

  9. NGINX的几个应用场景

    NGINX的几个应用场景 两个参考地址: NGINX的百度百科:https://baike.baidu.com/item/nginx/3817705?fr=aladdin NGINX的中文网站:htt ...

  10. CSS, JavaScript 压缩, 美化, 加密, 解密

    CSS, JavaScript 压缩, 美化, 加密, 解密 JS压缩, CSS压缩, javascript compress, js在线压缩,javascript在线压缩,css在线压缩,YUI C ...