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 正则表达式应用——缩写词扩充
看具体示例 import re def expand_abbr(sen, abbr): lenabbr = len(abbr) ma = '' for i in range(0, lenabbr): ...
- 周记5——随机撒花特效、动态修改伪元素样式、contenteditable属性、手机端调试利器VConsole、浏览器端debug调试
记录一些小零碎知识点,以便日后查看~ 1.随机撒花特效 教师节快到了,公司的产品提出一个需求:在IM(即时聊天)聊天界面弹出教师节的祝福“广告”,用户点击“发送祝福”按钮,聊天界面会随机撒花.这里的重 ...
- Golang教程:并发介绍
Go是并发语言,而不是并行语言.在讨论Go并发性之前,我们必须首先了解什么是并发,以及并发与并行的区别. 什么是并发 并发(concurrency)是指一次处理大量事情的能力.让我们用一个例子来说明. ...
- Firebird Fluentdata
Fluentdata 支持很多种数据库驱动,但对Firebird不友好,不过可以使用DB2Provider来操作大部分功能, 例如: new DbContext().ConnectionString( ...
- [转]让ASP.NET Web API支持$format参数的方法
本文转自:http://www.cnblogs.com/liuzhendong/p/4228592.html 在不使用OData的情况下,也可以让ASP.NET Web API支持$format参数, ...
- [转]Oracle中没有 if exists(...)
本文转自:http://blog.csdn.net/hollboy/article/details/7550171 对于Oracle中没有 if exists(...) 的语法,目前有许多种解决方法, ...
- django常用封装
#encoding:utf-8from django.shortcuts import render_to_responseimport hashlibfrom binascii import b2a ...
- <<数学传奇>>概述
简介 在网易公开课上看完了蔡天新教授的<<数学传奇>>,课程共10集.分别讲述了古希腊的毕达哥拉斯.波斯(伊朗)的 花拉子密和欧玛尔.海亚姆.中国的秦九韶.英国的牛顿.法国的笛 ...
- 纯手写实现HashMap
1.hashmap的实现 ① 初始化 1)定义一个Node<K, V>的数组来存放元素,但不立即初始化,在使用的时候再加载 2)定义数组初始大小为16 3)定义负载因子,默认为0.75, ...
- CXF生成客户端遇到的问题
一.CXF环境配置路径错误 1.错误现象 在命令行中输入 wsdl2java -v 检查CXF安装是否正确. 出现错误=> ERROR: Unable to find cxf-manifest. ...