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)的更多相关文章

  1. 【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 ...

  2. 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 ...

  3. 【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 ...

  4. Construct Binary Tree from Preorder and Inorder Traversal

    Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...

  5. 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 ...

  6. 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 ...

  7. 【题解二连发】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 ...

  8. LeetCode: Construct Binary Tree from Preorder and Inorder Traversal 解题报告

    Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...

  9. [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 ...

随机推荐

  1. 转 WINXP VBOX 给UBUNTU 加共享目录方法

    1. 安装增强功能包(Guest Additions)安装好Ubuntu 9.04后,运行Ubuntu并登录.然后在VirtualBox的菜单里选择"设备(Devices)" -& ...

  2. Unity游戏接入Steam成就

    在接入Steam成就,其实有些地方是有坑点的,而且steam官网给的是c++代码的接入教程.如果是老鸟的话,接入还并不是很难. 但是对于新手其实还是比较痛苦的,网上这方面的资料很少.这里我给总结下,u ...

  3. 一个Java小菜鸟的实习之路

    博主今年大四,六月份毕业,之前一直对编程感兴趣,于是在大学里自学了Java,(本专业是通信工程).在今年过年的时候,父母让来南方过年,于是博主自己也想着能不能在南方找份java的实习先干着,了解一下行 ...

  4. android studio应用获取系统属性权限(SystemProperties)

    dependencies { provided files(getLayoutLibPath()) } /** ZhangChao time:2014-12-31,get layoutlib.jar ...

  5. .net iis excel导出问题

    碰到几个问题的解决方法 1.当我远程服务器时才可以导出excel!!关闭了远程就不行... 解决:运行mmc -32组件服务 ->DCOM Config->Microsoft Excel  ...

  6. spring AOP Capability and Goals(面向方面编程功能和目标归纳)

    原官方文档链接: https://docs.spring.io/spring/docs/5.1.6.RELEASE/spring-framework-reference/core.html#aop-i ...

  7. 前端小结(4)---- 页面加载loding....

    /*正在加载*/ function showLoading(elem) { var html = '<div class="loading"><div id=&q ...

  8. Nginx 503错误总结

    nginx 503错误(Service Temporarily Unavailable  服务暂时不可用): 503是一种HTTP状态码,由于临时的服务器维护或者过载,服务器当前无法处理请求.这个状况 ...

  9. Js窗口嵌套

    <script type="text/javascript"> if (window.parent.window != window) { window.top.loc ...

  10. apache和tomcat搭建集群

    最近在学习简单的apache服务器和两个tomcat一起搭建集群,这里简单记录一下 1.准备工作 ①搭建一个可以运行的web项目 用maven搭建springmvc项目 ,只要将这里面的web.xml ...