Construct Binary Tree from Inorder and Postorder Traversal

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

 

Approach #1: C++.

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
        int len = inorder.size();
        unordered_map<int, int> mp;
        for (int i = 0; i < len; ++i)
            mp[inorder[i]] = i;
        return solve(inorder, 0, inorder.size()-1, postorder, 0, postorder.size()-1, mp);
    }
    
private:
    TreeNode* solve(vector<int>& inorder, int is, int ie, vector<int>& postorder, int ps, int pe, unordered_map<int, int> mp) {
        if (is > ie || ps > pe) return NULL;
        TreeNode* root = new TreeNode(postorder[pe]);
        int it = mp[postorder[pe]];
        TreeNode* leftchild = solve(inorder, is, it-1, postorder, ps, ps+it-is-1, mp);
        TreeNode* rightchild = solve(inorder, it+1, ie, postorder, ps+it-is, pe-1, mp);
        root->left = leftchild;
        root->right = rightchild;
        return root;
    }
};

  

Approach #2: Java.

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public TreeNode buildTree(int[] inorder, int[] postorder) {
        if (inorder == null || postorder == null || inorder.length != postorder.length) return null;
        HashMap<Integer, Integer> hm = new HashMap<Integer, Integer>();
        for (int i = 0; i < inorder.length; ++i) {
            hm.put(inorder[i], i);
        }
        return buildTreePostIn(inorder, 0, inorder.length-1, postorder, 0, postorder.length-1, hm);
    }
    
    private TreeNode buildTreePostIn(int[] inorder, int is, int ie, int[] postorder, int ps, int pe, HashMap<Integer, Integer> hm) {
        if (ps > pe || is > ie) return null;
        TreeNode root = new TreeNode(postorder[pe]);
        int ri = hm.get(postorder[pe]);
        TreeNode leftchild = buildTreePostIn(inorder, is, ri-1, postorder, ps, ps+ri-is-1, hm);
        TreeNode rightchild = buildTreePostIn(inorder, ri+1, ie, postorder, ps+ri-is, pe-1, hm);
        root.left = leftchild;
        root.right = rightchild;
        return root;
    }
}

  

Approach #3: Python.

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
    def buildTree(self, inorder, postorder):
        """
        :type inorder: List[int]
        :type postorder: List[int]
        :rtype: TreeNode
        """
        def helper(istart, iend, pstart, pend):
            if istart > iend or pstart > pend: return None
            root = TreeNode(postorder[pend])
            cur = idx[postorder[pend]]
            leftchild = helper(istart, cur-1, pstart, pstart+cur-istart-1)
            rightchild = helper(cur+1, iend, pstart+cur-istart, pend-1)
            root.left = leftchild
            root.right = rightchild
            return root
            
        idx = {}
        for i in range(len(inorder)):
            idx[inorder[i]] = i
        return helper(0, len(inorder)-1, 0, len(postorder)-1)
    

  

 

posted @ 2018-11-22 18:04  Veritas_des_Liberty  阅读(195)  评论(0编辑  收藏  举报