//根据一棵树的前序遍历与中序遍历构造二叉树。
//
// 注意:
//你可以假设树中没有重复的元素。
//
// 例如,给出
//
// 前序遍历 preorder = [3,9,20,15,7]
//中序遍历 inorder = [9,3,15,20,7]
//
// 返回如下的二叉树:
//
// 3
// / \
// 9 20
// / \
// 15 7
// Related Topics 树 深度优先搜索 数组
// 👍 893 👎 0
/*
* 105 从前序与中序遍历序列构造二叉树
* 2021-02-27 11:45:39
* @author oxygenbytes
*/
#include "leetcode.h"
//leetcode submit region begin(Prohibit modification and deletion)
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
unordered_map<int, int> mp;
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
int n = preorder.size();
if(!n) return nullptr;
for(int i = 0;i < inorder.size();i++) mp[inorder[i]] = i;
return dfs(preorder, inorder, 0, n - 1, 0 ,n - 1);
}
TreeNode* dfs(vector<int>& preorder, vector<int>& inorder, int pl, int pr, int il, int ir){
if (pl > pr) return nullptr;
int val = preorder[pl];
int k = mp[val];
int len = k - il;
auto root = new TreeNode(val);
root->left = dfs(preorder, inorder, pl + 1, pl + len, il, k - 1);
root->right = dfs(preorder, inorder, pl + len + 1, pr, k + 1, ir);
return root;
}
};
//leetcode submit region end(Prohibit modification and deletion)