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?

posted @ 2018-08-13 05:43  wz30  阅读(202)  评论(0编辑  收藏  举报