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 ...
随机推荐
- python中函数参数传递的几种方法
转自 http://www.douban.com/note/13413855/ Python中函数参数的传递是通过“赋值”来传递的.但这条规则只回答了函数参数传递的“战略问题”,并没有回答“战术问题 ...
- Python生成pyc文件
Python生成pyc文件 pyc文件是py文件编译后生成的字节码文件(byte code).pyc文件经过python解释器最终会生成机器码运行.所以pyc文件是可以跨平台部署的,类似Java的.c ...
- 通过反射获取及调用方法(Method)
1.获取方法使用反射获取某一个类中的方法,步骤:①找到获取方法所在类的字节码对象②找到需要被获取的方法 Class类中常用方法: public Method[] getMethods():获取包括自身 ...
- css选择器星号(*)
1.星号(*)选择器的优先级 css的(*)选择器,也是通用选择器,对所有的页面元素(html,title,style,body,div,p……)应用样式,级别最低 在选择器的级别中:在元素名< ...
- whatwg-fetch
fetch 是什么 XMLHttpRequest的最新替代技术 fetch优点 接口更简单.简洁,更加语义化 基于promise,更加好的流程化控制,可以不断then把参数传递,外加 async/aw ...
- python 包管理工具Pipenv
Kenneth Reitz的最新工具Pipenv可以用于简化Python项目中依赖项的管理. 它汇集了Pip,Pipfile和Virtualenv的功能,是一个强大的命令行工具. 入门 首先使用pip ...
- MDI-设置子窗体只能弹出一个--单例模式
不足之处,欢迎指正! 什么是MDI..我表示不知道的呢. MDI(Multiple Document Interface)就是所谓的多文档界面,与此对应就有单文档界面 (SDI), 它是微软公司从Wi ...
- 多个tomcat配置
在centos7.3下搭建jenkins自动部署环境,需要一个tomcat来启动jenkins,另一个用来自动部署的位置,因此需要两个tomcat同时运行,并且在自动构建后能够启动项目,又不会关闭je ...
- Spring_Spring与IoC_基于XML的DI
一.注入分类 bean实例在调用无参构造器创建空值对象后,就要对Bean对象的属性进行初始化.初始化时由容器自动完成的,称为注入.根据注入方式的不同,常用的有2类:设值注入.构造注入.(还有一种,实现 ...
- POJ 2955 Brackets 区间DP 最大括号匹配
http://blog.csdn.net/libin56842/article/details/9673239 http://www.cnblogs.com/ACMan/archive/2012/08 ...