105 + 106. Construct Binary Tree from Preorder and Inorder Traversal (building trees)
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
Solution: build tree, how to divide the array for each root and subtree
we can use map to get the index of the array for a specific element
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode buildTree(int[] preorder, int[] inorder) {
//prestart : root
return buildtree(preorder, inorder, 0, 0, inorder.length - 1);
}
public TreeNode buildtree( int[] preorder, int[] inorder,int prestart, int instart, int inend) {
//boundary situation 1. pre > 2. instart and inend
if(instart > inend || prestart > preorder.length -1)
return null; TreeNode node = new TreeNode(preorder[prestart]); //root int inindex = 0 ; //for the inorder[inindex] == preorder[prestart]
for(int i = instart ; i <= inend; i++) {
if(inorder[i] == preorder[prestart])
inindex = i;
} node.left = buildtree( preorder, inorder, prestart+1, instart, inindex -1); //
node.right = buildtree( preorder, inorder, prestart+inindex-instart+1, inindex+1, inend); //prestart : pass the number of the left subtrss + current node
return node;
}
}
//time: o(n^2) space O(n)
/*
The basic idea is here:
Say we have 2 arrays, PRE and IN.
Preorder traversing implies that PRE[0] is the root node.
Then we can find this PRE[0] in IN, say it's IN[5].
Now we know that IN[5] is root, so we know that IN[0] - IN[4] is on the left side, IN[6] to the end is on the right side.
Recursively doing this on subarrays, we can build a tree out of it :)
*/
//from bottom to up
//reference
//http://www.geeksforgeeks.org/construct-tree-from-given-inorder-and-preorder-traversal/ //e.g. Inorder sequence: D B E A F C
// Preorder sequence: A B D E C F
Solution 2: using hashmap , reduce the time, but add apce
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
Map<Integer, Integer> map;
public TreeNode buildTree(int[] preorder, int[] inorder) {
if(inorder.length==0) return null;
map = new HashMap<>();
for(int i = 0; i<inorder.length; i++){
map.put(inorder[i], i);
}
return helper(preorder, inorder, 0, 0, inorder.length);
}
TreeNode helper(int[] preorder, int[] inorder, int preIndex,int inStart, int inEnd){
if(inStart >= inEnd || preIndex > preorder.length-1){
return null;
}
TreeNode root = new TreeNode(preorder[preIndex]);
int newIndex = 0;
newIndex = map.get(preorder[preIndex]); root.left = helper(preorder, inorder, preIndex+1, inStart, newIndex);
root.right = helper(preorder, inorder, preIndex+newIndex - inStart+1, newIndex+1, inEnd);
return root;
} }
106 :
Given inorder and postorder traversal of a tree, construct the binary tree. Note:
You may assume that duplicates do not exist in the tree. For example, given inorder = [9,3,15,20,7]
postorder = [9,15,7,20,3]
Return the following binary tree: 3
/ \
9 20
/ \
15 7
Solution: only attach the hashmap method
Difference: index of root, left subtree, right subtree changed
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
Map<Integer, Integer> map;
public TreeNode buildTree(int[] inorder, int[] postorder) {
if(inorder.length==0) return null;
map = new HashMap<>();
for(int i = 0; i < inorder.length; i++){
map.put(inorder[i], i);
}
return helper(inorder, postorder,postorder.length-1, 0, inorder.length-1 );
}
TreeNode helper(int[] inorder, int[] postorder, int postIndex,int inStart,int inEnd){
if(inStart > inEnd || postIndex <0){
return null;
}
int newIndex = map.get(postorder[postIndex]);
TreeNode root = new TreeNode(postorder[postIndex]);
root.left = helper(inorder, postorder, postIndex-(inEnd - newIndex+1), inStart, newIndex-1);
root.right = helper(inorder, postorder, postIndex-1, newIndex+1, inEnd);
return root;
}
}
Follow up: what if there are duplicate elements?
105 + 106. Construct Binary Tree from Preorder and Inorder Traversal (building trees)的更多相关文章
- 【LeetCode】105 & 106 Construct Binary Tree from (Preorder and Inorder) || (Inorder and Postorder)Traversal
Description: Given arrays recording 'Preorder and Inorder' Traversal (Problem 105) or 'Inorder and ...
- 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 t ...
- 【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 ...
- Construct Binary Tree from Preorder and Inorder Traversal
Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...
- 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 ...
- 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 ...
- 【题解二连发】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 ...
- LeetCode: 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 ...
随机推荐
- echart使用设置一个柱形的最小宽度
因为echart的横坐标的个数不同会影响柱形图的宽度 如果只有三个月的就会是这样的 这样一来效果就不是很好,所以想做成如下效果 思路: 只是需要向xDate的值设置成想要的长度,如上图就是设置12,如 ...
- 【Qt开发】更改应用程序图标和任务栏图标
说明 实际开发过程中,生成的应用文件不会用默认的图标,同时程序启动后任务栏的图标也需要修改,还有窗口的图标,这样显得程序不那么low.更改程序的图标有多种方式,基于Qt Creator或vs开发的方式 ...
- LoadRunner使用
LoadRunner使用 软件版本:12.53 build 1203 操作系统: Windows7 以下内容摘录自LoadRunner的官方帮助文档. 介绍 LoadRunner现在是HP公司的产品, ...
- java ee的map
- 从外网GitHub clone开源项目的时候,.git文件过大,导致克隆慢
以clone impala为例,主要是加入-depth=1参数: git clone -b cdh4-2.0 --depth=1 https://github.com/cloudera/Impala. ...
- HDU 5690——All X——————【快速幂 | 循环节】
All X Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submi ...
- Firebird 有用的list函数
语法: LIST ([ALL | DISTINCT] expression [, separator]) 示例: select list(u.code, ';') from m_user u 结果:查 ...
- Splunk和ELK深度对比
转自:http://blog.51cto.com/splunkchina/1948105 日志处理两大生态Splunk和ELK深度对比 heijunmasd 0人评论 5312人阅读 2017-07- ...
- ASP.NET MVC4 新手入门教程之六 ---6.编辑视图与编辑方法
在本节中,您会为电影控制器检查生成的操作方法和视图.然后,您将添加一个自定义的搜索页面. 运行该应用程序,然后浏览到Movies控制器通过将/Movies追加到您的浏览器的地址栏中的 URL.将鼠标指 ...
- 三:SpringTransaction
一:什么是事务: 事务逻辑上的一组操作,组成这组操作的各个逻辑单元,要么一起成功,要么一起失败. 二:事务特性(ACID): 原子性(Atomicity) :强调事务的不可分割. 一致性(Consis ...