Question:


Given inorder and postorder traversal of a tree, construct the binary tree.



Note:



You may assume that duplicates do not exist in the tree.


Anwser 1 :

/**
  * Definition for binary tree
  * struct TreeNode {
  *     int val;
  *     TreeNode *left;
  *     TreeNode *right;
  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
  * };
  */
 class Solution {
 public:
     TreeNode *createTree(vector<int> &inorder, int inBeg, int inEnd, vector<int> &postorder, int postBeg, int postEnd)
     {
         if (inBeg > inEnd)  return NULL;
             
         int root = postorder[postEnd];
         
         int index;
         
         for(int i = inBeg; i <= inEnd; i++)
             if (inorder[i] == root)
             {
                 index = i;
                 break;
             }
             
         int len = index - inBeg;
         TreeNode *left = createTree(inorder, inBeg, index - 1, postorder, postBeg, postBeg + len - 1);
         TreeNode *right = createTree(inorder, index + 1, inEnd, postorder, postBeg + len, postEnd - 1);
         
         TreeNode *node = new TreeNode(root);
         node->left = left;
         node->right = right;
         
         return node;
     }
     
     TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) {
         // Start typing your C/C++ solution below
         // DO NOT write int main() function
         if (inorder.size() == 0) return NULL;
             
         return createTree(inorder, 0, inorder.size() - 1, postorder, 0, postorder.size() - 1);
     }
 };


Anwser 2 :

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    std::unordered_map<int,int> index_map;

    void buildMap(vector<int> &inorder) {
        index_map.clear();
        for (int i = 0; i < inorder.size(); i++) {
            index_map[inorder[i]] = i;
        }
    }

    TreeNode *buildTreeInOrder(vector<int> &postorder, int post_offset, vector<int> &inorder, int in_offset, int len) 
    {
        if (!len)  return NULL;

        TreeNode *root = new TreeNode(postorder[post_offset]);
        int i = index_map[postorder[post_offset]];

        root->right = buildTreeInOrder(postorder, post_offset-1, inorder, in_offset, in_offset-i);
        root->left = buildTreeInOrder(postorder, post_offset-(in_offset-i)-1, inorder, in_offset-(in_offset-i)-1, len-(in_offset-i)-1);

        return root;
    }

    TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        if (postorder.size() == 0 && inorder.size() == 0) {
            return NULL;
        }

        buildMap(inorder);
        return buildTreeInOrder(postorder, postorder.size()-1, inorder, inorder.size()-1, postorder.size());
    }
};



参考推荐:


Construct Binary Tree from Inorder and Postorder Traversal