Skip to content

Latest commit

 

History

History
65 lines (58 loc) · 1.6 KB

106.Construct Binary Tree from Inorder and Postorder Traversal.md

File metadata and controls

65 lines (58 loc) · 1.6 KB

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
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public TreeNode buildTree(int[] inorder, int[] postorder) {
        Map<Integer,Integer> map=new HashMap<>();
        for(int i=0;i<inorder.length;i++){
            map.put(inorder[i],i);
        }
        return helper(inorder,postorder,0,inorder.length-1,0,postorder.length-1,map);  
    }  
    public TreeNode helper(int[] inorder,int[] postorder,int inS,int inE,int pS,int pE,Map<Integer,Integer> map){
        if(inS>inE) return null;
        int rootIdx=-1;
        int rootVal=postorder[pE];
        rootIdx=map.get(rootVal);
        int ils = inS;
        int ire = inE;
        int pls = pS;
        int pre = pE-1;
        int ile = rootIdx-1;
        int irs = rootIdx+1;
        int ple = ile-ils+pls;
        int prs = ple+1;
        TreeNode root=new TreeNode(rootVal);
        root.left=helper(inorder,postorder,ils,ile,pls,ple,map);
        root.right=helper(inorder,postorder,irs,ire,prs,pre,map);
        return root;
    }
}