aboutsummaryrefslogtreecommitdiff
path: root/construct_bt_io_po.cpp
blob: 6fb91637a2b9d97ae420b74b6b3ca552c0a4ead7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// 106. Construct Binary Tree from Inorder and Postorder Traversal
#include "leetcode.h"

/*
 * given two integer arrays 'inorder' and 'postorder', where 'inorder' is the
 * inorder traversal of a binary tree, and 'postorder' is the postorder
 * traversal of the same tree, construct and return the binary tree.
 */

class Solution {
public:
  TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) {
    int n = inorder.size(), po_idx = n - 1;
    for (int i = 0; i < n; i++)
      um[inorder[i]] = i;
    return construct(inorder, postorder, 0, n - 1, po_idx);
  }

private:
  unordered_map<int, int> um;
  TreeNode *construct(vector<int> &inorder, vector<int> &postorder,
                      int io_start, int io_end, int po_idx) {
    if (io_start > io_end)
      return nullptr;
    TreeNode *root = new TreeNode(postorder[po_idx--]);
    int io_idx = um[root->val];
    root->right = construct(inorder, postorder, io_idx + 1, io_end, po_idx);
    root->left = construct(inorder, postorder, io_start, io_idx - 1, po_idx);
    return root;
  }
};