[LC] 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 duplicates do not exist in the tree.
For example, given
preorder = [3,9,20,15,7]
inorder = [9,3,15,20,7]
Return the following binary tree:
3
/ \
9 20
/ \
15 7
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode buildTree(int[] preorder, int[] inorder) {
Map<Integer, Integer> mymap = new HashMap<>();
for (int i = 0; i < inorder.length; i++) {
mymap.put(inorder[i], i);
}
return helper(0, inorder.length - 1, 0, preorder.length - 1, preorder, mymap);
} private TreeNode helper(int inLeft, int inRight, int preLeft, int preRight, int[] preorder, Map<Integer, Integer> mymap) {
if (inLeft > inRight) {
return null;
}
TreeNode cur = new TreeNode(preorder[preLeft]);
int leftSize = mymap.get(preorder[preLeft]) - inLeft;
// preRight for left just add up leftSize
cur.left = helper(inLeft, inLeft + leftSize - 1, preLeft + 1, preLeft + leftSize, preorder, mymap);
cur.right = helper(inLeft + leftSize + 1, inRight, preLeft + leftSize + 1, preRight, preorder, mymap);
return cur;
}
}
[LC] 105. Construct Binary Tree from Preorder and Inorder Traversal的更多相关文章
- 【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 ...
- [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 ...
- 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 ...
- 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 ...
- 105. Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree. ============== 基本功: 利用前序和 ...
- 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 ...
- 【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 ...
- 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 ...
- 【一天一道LeetCode】#105. Construct Binary Tree from Preorder and Inorder Traversal
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源:http ...
随机推荐
- JavaScript 之 "for"的衍生对象
JavaScript for/in 语句 作用:for/in 语句用于遍历循环对象属性. 循环中的代码每执行一次,就会对数组的元素或者对象的属性进行一次操作. 例子: 循环对象属性: var pers ...
- 如何用Python统计《论语》中每个字的出现次数?10行代码搞定--用计算机学国学
编者按: 上学时听过山师王志民先生一场讲座,说每个人不论干什么,都应该学习国学(原谅我学了计算机专业)!王先生讲得很是吸引我这个工科男,可能比我的后来的那些同学听课还要认真些,当然一方面是兴趣.一方面 ...
- Asp.NET CORE安装部署
先安装IIS再安装这两个,不然后面各种bug HTTP 错误 500.19 代码 0x8007000d 解决方案 for win7_64 asp.net core IIS Web Core 1.比如最 ...
- 微信小程序下载图片到本地
downloadImg: function(e){ //触发函数 console.log(e.currentTarget.dataset.url) wx.downloadFile({ url: e.c ...
- 操作实践:Java桌面程序实现日志级别热修改
声明:迁移自本人CSDN博客https://blog.csdn.net/u013365635 定位问题的时候往往需要动态修改日志级别并且不能影响业务的正常运行,也就是不能重启应用,此时就要使用到动态日 ...
- 寒假day09
今天看了论文的结构,定下了毕设论文的框架,刷了剑指offer的部分算法题.
- 【转帖】使用容器化和 Docker 实现 DevOps 的基础知识
使用容器化和 Docker 实现 DevOps 的基础知识 https://www.kubernetes.org.cn/6730.html 2020-02-24 15:20 灵雀云 分类:容器 阅读( ...
- idea抛出异常:org.apache.shiro.authc.AuthenticationException
问题描述 继续在ubuntu下折腾,终于将web项目的所有的东西配置好了,然后运行项目,满怀期待的心情登录系统的时候,突然出现了这个bug,吓得我差点从椅子上跳起来,这两天遇到的bug实在是太多了.. ...
- runlevel 运行级别
linux启动过程 关于Ubuntu 12.04修改默认运行级别,启动字符界面的个人理解 网上通常的做法是:(亲自试验,不管用),如果想直接操作请看绿色字体部分 (1)第一种方法: 由于Red ...
- 如何解决Tomcat端口号被占用
在eclipse中配置好tomcat服务器后,启动时提示错误如下图 提示端口被占用. 第一种方法: 结束占用端口的进程 第一步:netstat -aon|findstr "端口号" ...