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 ...
随机推荐
- 虚拟机下linux 的root密码忘记怎么修改(转)
1.开机时任意按一个方向键,进入界面,选择linux系统,按e键进入 2.然后用上下键选择kerner(内核)那一行,按e键进入编辑界面,编辑界面最后一行显示如下:(grub edit> ker ...
- 大型网站技术学习-3. 容器Docker与kubernetes
大型网站技术基石篇-容器Docker与kubernetes Docker和Kubernetes的关系就如Xen与OpenStack. Docker是一种容器技术,和Hypervisor(KVM/X ...
- 第一次尝试用ANT进行build
虽然是软件工程专业学生,但很多东西都才刚刚接触,有些惭愧,但我相信“Later better than never”,所以我还是鼓励自己不断学习,以后尽量把自己新学会的东西记录下来,以此来督促自己的学 ...
- System.Web.Mvc.HtmlHelper<dynamic>”没有名为“Partial”的适用方法,但似乎有一个具有该名称的扩展方法。扩展方法不能进行动态调度。请考虑强制转换动态参数,或调用该扩展方法但不使用扩展方法语法。
MVC 调用分布式图,传了没有定义的参数,,参数写得不对
- JavaScript中 运算符
运算符对一个或多个变量或值(操作数)进行运算,并返回一个新值 根据所执行的运算,运算符可分为以下类别: (1) 算术运算符 (2) 比较运算符 运算符 说 明 示 例 == 等于. 如果两个操作数相 ...
- @RequestBody与serialize()、serializeArray()、拼接Json 妙用总结
@requestBody注解常用来处理content-type不是默认的application/x-www-form-urlcoded编码的内容, 比如说:application/json或者是app ...
- JFrame自适应大小
pack();函数调用PreferedSize(); 所以对于组件要setPreferedSize();
- Redis数据类型及常用命名总结
Redis数据类型: Redis支持五种数据类型:string(字符串),hash(哈希),list(列表),set(集合)及zset(sorted set:有序集合). 1.String(字符串) ...
- Effective C++ .06 阻止编译器自动生成函数以及被他人调用
这节讲了下如何防止对象拷贝(隐藏并不能被其他人调用) 两种方法: 1. 将拷贝构造函数声明为private 并且声明函数但不进行定义 #include <iostream> #includ ...
- css多行本文垂直集中
<div style="display:table;height:400px;"> <span style="display:table-cell;ve ...