题目链接:106. 从中序与后序遍历序列构造二叉树

题解:

二叉树的后中序构造二叉树!

题目简述:

给定二叉树的后中序遍历来构造一颗二叉树!

题解:

  • 后序遍历:根节点最后遍历
  • 中序遍历:通过后序遍历得到的值找到中序序列的根节点下标位置,将序列分为左右子树

与前中序一样,详细见上一道题!

这里只给出下标对应关系:

  • 左子树下标: pl, k - 1 - il + pl, il, k - 1
  • 右子树下标: k - 1 - il + pl + 1, pr - 1, k + 1, ir

下标计算同上一道前中序计算,参考上一题!

时间复杂度:同样适用哈希表将复杂度降为O(n)

AC代码:

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
/**
* 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:
unordered_map<int, int> pos;
TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
for(int i = 0; i < inorder.size(); i++) pos[inorder[i]] = i;
return build(postorder, inorder, 0, postorder.size() - 1, 0, inorder.size() - 1);
}
TreeNode* build(vector<int>& postorder, vector<int>& inorder, int pl, int pr, int il, int ir){
if(pl > pr) return NULL;
auto root = new TreeNode(postorder[pr]);
int k = pos[root->val];
root->left = build(postorder, inorder, pl, k - 1 - il + pl, il, k - 1);
root->right = build(postorder, inorder, k - 1 - il + pl + 1, pr - 1, k + 1, ir);
return root;
}
};