从中序和后序遍历序列构造二叉树
题目链接
class Solution {
public:unordered_map<int,int> hash;TreeNode* build(int rooti,int left,int right,vector<int>& postorder){if(left > right)return nullptr;TreeNode* node = new TreeNode(postorder[rooti]);int index = hash[postorder[rooti]];node->left = build(rooti-1-(right-index),left,index-1,postorder);node->right = build(rooti-1,index+1,right,postorder);return node;}TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {for(int i = 0;i < inorder.size();i++){hash[inorder[i]] = i;}return build(postorder.size()-1,0,inorder.size()-1,postorder);}
};