从前(或后)与中序序列构建二叉树
Leetcode 106. Construct Binary Tree from Inorder and Postorder
Leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal两道题是从前中后序列回复原二叉树,是二叉树数据结构常见的题型,现在分析解决下。
1. 基本思路分析
- 首先明确二叉树的前中后序遍历的概念。可知前序遍历的第一个元素即根结点的关键字,后序遍历的最后一个元素即根结点的关键字,找出了根结点,再在中序遍历序列中确定左右子树的序列找出,同时在对应的前或后序列中找出相应子序列,那么重复上述思路实现根结点的左右子树的构建。
- 只有前后序列无法确定左右子树,故此条件无法构建原二叉树。1
举例说明,
先序:abdgcefh—>a bdg cefh
中序:dgbaechf---->dgb a echf
得出:a是树根,a有左子树和右子树,左子树有bdg结点,右子树有cefh结点。
在对左右子树递归上述分析,左子树的
先序:bdg—>b dg
中序:dgb —>dg b
得出结论:d是b左子树的根节点,d无左子树,g是d的右子树
2. 代码实现
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
//从前中序构建二叉树
TreeNode *BuildTreeFromPreorderInorder(vector<int> &preorder,
vector<int> &inorder) {
if (preorder.empty()) {
return NULL;
}
int key = preorder[0];
size_t n = preorder.size();
TreeNode *root = new TreeNode(key);
vector<int> l_pre, l_in, r_pre, r_in;
int tmp = inorder[0];
int b = 0;
while (tmp != key) {
l_in.push_back(tmp);
tmp = inorder[++b];
}
size_t nl = l_in.size();
for (size_t i = 0; i < nl; i++) {
l_pre.push_back(preorder[i + 1]);
}
for (size_t i = b + 1; i < n; i++) {
r_in.push_back(inorder[i]);
}
for (size_t i = nl + 1; i < n; i++) {
r_pre.push_back(preorder[i]);
}
root->left = BuildTreeFromPreorderInorder(l_pre, l_in);
root->right = BuildTreeFromPreorderInorder(r_pre, r_in);
return root;
}
//从中后序构建二叉树
TreeNode *BuildTreeFromInorderPostorder(vector<int> &inorder,
vector<int> &postorder) {
if (inorder.empty()) {
return NULL;
}
auto key = *(postorder.end() - 1);
TreeNode *root = new TreeNode(key);
// vector<int>::iterator bin = find(inorder.begin(), inorder.end(), key);
vector<int> new_l_in, new_r_in, new_l_post, new_r_post;
// TODO: debug
// copy(inorder.begin(), bin, new_l_in.begin());
int tmp = inorder[0];
int nb = 0;
while (tmp != key) {
new_l_in.push_back(tmp);
tmp = inorder[++nb];
}
auto n = inorder.size();
auto num_l = new_l_in.size();
for (size_t i = 0; i < num_l; i++) {
new_l_post.push_back(postorder[i]);
}
// copy(bin + 1, inorder.end(), new_r_in.begin());
for (size_t i = num_l; i < n - 1; i++) {
new_r_post.push_back(postorder[i]);
}
for (size_t i = num_l + 1; i < n; i++) {
new_r_in.push_back(inorder[i]);
}
root->left = BuildTreeFromInorderPostorder(new_l_in, new_l_post);
root->right = BuildTreeFromInorderPostorder(new_r_in, new_r_post);
return root;
3. 更快的解法
class Solution {
public:
// use recursion to construct the tree
// 1. use preorder to confirm the root
// 2. use inorder to get the boundary of left subtree and right subtree
int pIndex = 0;
int iIndex = 0;
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
if (preorder.size() == 0 || inorder.size() == 0) return NULL;
return helper(preorder, inorder, INT_MIN);
}
// nextInorderVal: the next value after visiting the current subtree in inorder order.
TreeNode* helper(vector<int>& preorder, vector<int>& inorder, int nextInorderVal) {
// base case
if (pIndex == preorder.size() || inorder[iIndex] == nextInorderVal) { return NULL; }
// consturct the root
TreeNode* root = new TreeNode(preorder[pIndex++]);
root->left = helper(preorder, inorder, root->val);
// inorder index
iIndex++;
root->right = helper(preorder, inorder, nextInorderVal);
return root;
}
};
https://blog.csdn.net/wumuzi520/article/details/8078322 ↩︎