如何根据先序和中序遍历结果重建特定的二叉树?
- 内容介绍
- 文章标签
- 相关推荐
本文共计962个文字,预计阅读时间需要4分钟。
本例介绍了C++中基于先序遍历和中序遍历结果重建二叉树的方法。以下是一个简化的示例:
cpp#include #include
using namespace std;
// 定义二叉树节点结构struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}};
// 根据先序和中序遍历结果重建二叉树TreeNode* buildTree(vector& preorder, vector& inorder) { if (preorder.empty() || inorder.empty()) return nullptr;
// 先序遍历的第一个元素是根节点 TreeNode* root=new TreeNode(preorder[0]); int rootIndex=find(inorder.begin(), inorder.end(), preorder[0]) - inorder.begin();
// 递归重建左子树和右子树 root->left=buildTree(vector(preorder.begin() + 1, preorder.begin() + 1 + rootIndex), vector(inorder.begin(), inorder.begin() + rootIndex)); root->right=buildTree(vector(preorder.begin() + 1 + rootIndex, preorder.end()), vector(inorder.begin() + rootIndex + 1, inorder.end()));
return root;}
// 辅助函数:在inorder中查找val的索引int find(vector& inorder, int val) { for (int i=0; i // 辅助函数:打印二叉树(后序遍历)void printPostorder(TreeNode* root) { if (root) { printPostorder(root->left); printPostorder(root->right); cout int main() { // 示例输入 vector preorder={3, 9, 20, 15, 7}; vector inorder={9, 3, 15, 20, 7}; // 重建二叉树 TreeNode* root=buildTree(preorder, inorder); // 打印重建的二叉树的后序遍历结果 printPostorder(root); cout < return 0;} 这段代码定义了一个二叉树节点结构,并实现了基于先序和中序遍历结果重建二叉树的功能。它还包括一个辅助函数来在后序遍历中打印二叉树的值。主函数中给出了一个示例输入,并展示了如何使用这些函数来重建二叉树并打印其后序遍历结果。 本文实例讲述了C++基于先序、中序遍历结果重建二叉树的方法。分享给大家供大家参考,具体如下: 题目: 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建二叉树并返回。 实现代码:
#include <iostream>
#include <vector>
#include <stack>
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
//创建二叉树算法
TreeNode* reConstructBinaryTree(vector<int> pre, vector<int> mid)
{
int nodeSize = mid.size();
if (nodeSize == 0)
return NULL;
vector<int> leftPre, leftMid, rightPre, rightMid;
TreeNode* phead = new TreeNode(pre[0]); //第一个当是根节点
int rootPos = 0; //根节点在中序遍历中的位置
for (int i = 0; i < nodeSize; i++)
{
if (mid[i] == pre[0])
{
rootPos = i;
break;
}
}
for (int i = 0; i < nodeSize; i++)
{
if (i < rootPos)
{
leftMid.push_back(mid[i]);
leftPre.push_back(pre[i + 1]);
}
else if (i > rootPos)
{
rightMid.push_back(mid[i]);
rightPre.push_back(pre[i]);
}
}
phead->left = reConstructBinaryTree(leftPre, leftMid);
phead->right = reConstructBinaryTree(rightPre, rightMid);
return phead;
}
//打印后续遍历顺序
void printNodeValue(TreeNode* root)
{
if (!root){
return;
}
printNodeValue(root->left);
printNodeValue(root->right);
cout << root->val<< " ";
}
int main()
{
vector<int> preVec{ 1, 2, 4, 5, 3, 6 };
vector<int> midVec{ 4, 2, 5, 1, 6, 3 };
cout << "先序遍历序列为 1 2 4 5 3 6" << endl;
cout << "中序遍历序列为 4 2 5 1 6 3" << endl;
TreeNode* root = reConstructBinaryTree(preVec, midVec);
cout << "后续遍历序列为 ";
printNodeValue(root);
cout << endl;
system("pause");
}
/*
测试二叉树形状:
1
2 3
4 5 6
*/
运行结果:
先序遍历序列为 1 2 4 5 3 6
中序遍历序列为 4 2 5 1 6 3
后续遍历序列为 4 5 2 6 3 1
请按任意键继续. . .
希望本文所述对大家C++程序设计有所帮助。
本文共计962个文字,预计阅读时间需要4分钟。
本例介绍了C++中基于先序遍历和中序遍历结果重建二叉树的方法。以下是一个简化的示例:
cpp#include #include
using namespace std;
// 定义二叉树节点结构struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}};
// 根据先序和中序遍历结果重建二叉树TreeNode* buildTree(vector& preorder, vector& inorder) { if (preorder.empty() || inorder.empty()) return nullptr;
// 先序遍历的第一个元素是根节点 TreeNode* root=new TreeNode(preorder[0]); int rootIndex=find(inorder.begin(), inorder.end(), preorder[0]) - inorder.begin();
// 递归重建左子树和右子树 root->left=buildTree(vector(preorder.begin() + 1, preorder.begin() + 1 + rootIndex), vector(inorder.begin(), inorder.begin() + rootIndex)); root->right=buildTree(vector(preorder.begin() + 1 + rootIndex, preorder.end()), vector(inorder.begin() + rootIndex + 1, inorder.end()));
return root;}
// 辅助函数:在inorder中查找val的索引int find(vector& inorder, int val) { for (int i=0; i // 辅助函数:打印二叉树(后序遍历)void printPostorder(TreeNode* root) { if (root) { printPostorder(root->left); printPostorder(root->right); cout int main() { // 示例输入 vector preorder={3, 9, 20, 15, 7}; vector inorder={9, 3, 15, 20, 7}; // 重建二叉树 TreeNode* root=buildTree(preorder, inorder); // 打印重建的二叉树的后序遍历结果 printPostorder(root); cout < return 0;} 这段代码定义了一个二叉树节点结构,并实现了基于先序和中序遍历结果重建二叉树的功能。它还包括一个辅助函数来在后序遍历中打印二叉树的值。主函数中给出了一个示例输入,并展示了如何使用这些函数来重建二叉树并打印其后序遍历结果。 本文实例讲述了C++基于先序、中序遍历结果重建二叉树的方法。分享给大家供大家参考,具体如下: 题目: 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建二叉树并返回。 实现代码:
#include <iostream>
#include <vector>
#include <stack>
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
//创建二叉树算法
TreeNode* reConstructBinaryTree(vector<int> pre, vector<int> mid)
{
int nodeSize = mid.size();
if (nodeSize == 0)
return NULL;
vector<int> leftPre, leftMid, rightPre, rightMid;
TreeNode* phead = new TreeNode(pre[0]); //第一个当是根节点
int rootPos = 0; //根节点在中序遍历中的位置
for (int i = 0; i < nodeSize; i++)
{
if (mid[i] == pre[0])
{
rootPos = i;
break;
}
}
for (int i = 0; i < nodeSize; i++)
{
if (i < rootPos)
{
leftMid.push_back(mid[i]);
leftPre.push_back(pre[i + 1]);
}
else if (i > rootPos)
{
rightMid.push_back(mid[i]);
rightPre.push_back(pre[i]);
}
}
phead->left = reConstructBinaryTree(leftPre, leftMid);
phead->right = reConstructBinaryTree(rightPre, rightMid);
return phead;
}
//打印后续遍历顺序
void printNodeValue(TreeNode* root)
{
if (!root){
return;
}
printNodeValue(root->left);
printNodeValue(root->right);
cout << root->val<< " ";
}
int main()
{
vector<int> preVec{ 1, 2, 4, 5, 3, 6 };
vector<int> midVec{ 4, 2, 5, 1, 6, 3 };
cout << "先序遍历序列为 1 2 4 5 3 6" << endl;
cout << "中序遍历序列为 4 2 5 1 6 3" << endl;
TreeNode* root = reConstructBinaryTree(preVec, midVec);
cout << "后续遍历序列为 ";
printNodeValue(root);
cout << endl;
system("pause");
}
/*
测试二叉树形状:
1
2 3
4 5 6
*/
运行结果:
先序遍历序列为 1 2 4 5 3 6
中序遍历序列为 4 2 5 1 6 3
后续遍历序列为 4 5 2 6 3 1
请按任意键继续. . .
希望本文所述对大家C++程序设计有所帮助。

