C++编写程序,输入先序和中序遍历,构建二叉树,给出该二叉树的层序遍历,并可以输入一个节点的值,完成在该树中搜索该节点,并输出其路径。
时间: 2024-03-24 22:37:08 浏览: 74
好的,下面是C++实现输入先序和中序遍历,构建二叉树,给出该二叉树的层序遍历,并可以输入一个节点的值,完成在该树中搜索该节点,并输出其路径的程序:
```c++
#include <iostream>
#include <vector>
#include <queue>
#include <unordered_map>
using namespace std;
// 二叉树节点结构体
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};
// 递归函数,根据先序遍历和中序遍历构建二叉树
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder, int preStart, int preEnd, int inStart, int inEnd) {
if (preStart > preEnd || inStart > inEnd) {
return nullptr; // 递归终止条件
}
int rootVal = preorder[preStart]; // 先序遍历的第一个节点为根节点
int rootIndex = inStart; // 在中序遍历中找到根节点
while (rootIndex <= inEnd && inorder[rootIndex] != rootVal) {
rootIndex++;
}
int leftSize = rootIndex - inStart; // 左子树的节点数
TreeNode* root = new TreeNode(rootVal); // 创建根节点
root->left = buildTree(preorder, inorder, preStart + 1, preStart + leftSize, inStart, rootIndex - 1); // 递归构建左子树
root->right = buildTree(preorder, inorder, preStart + leftSize + 1, preEnd, rootIndex + 1, inEnd); // 递归构建右子树
return root;
}
// 层序遍历,返回结果为vector<int>
vector<int> levelOrder(TreeNode* root) {
vector<int> result;
if (root == nullptr) {
return result;
}
queue<TreeNode*> q;
q.push(root);
while (!q.empty()) {
int size = q.size();
for (int i = 0; i < size; i++) {
TreeNode* node = q.front();
q.pop();
result.push_back(node->val);
if (node->left != nullptr) {
q.push(node->left);
}
if (node->right != nullptr) {
q.push(node->right);
}
}
}
return result;
}
// 搜索节点,并返回路径,使用unordered_map记录当前节点的父节点
vector<TreeNode*> searchNode(TreeNode* root, int val, unordered_map<TreeNode*, TreeNode*>& parent) {
vector<TreeNode*> path;
if (root == nullptr) {
return path; // 空树返回空路径
}
queue<TreeNode*> q;
q.push(root);
while (!q.empty()) {
int size = q.size();
for (int i = 0; i < size; i++) {
TreeNode* node = q.front();
q.pop();
if (node->val == val) {
while (node != nullptr) {
path.push_back(node);
node = parent[node];
}
reverse(path.begin(), path.end()); // 反转路径
return path; // 找到节点,返回路径
}
if (node->left != nullptr) {
parent[node->left] = node; // 记录左子节点的父节点
q.push(node->left);
}
if (node->right != nullptr) {
parent[node->right] = node; // 记录右子节点的父节点
q.push(node->right);
}
}
}
return path; // 没有找到节点,返回空路径
}
int main() {
vector<int> preorder = {1, 2, 4, 5, 3, 6};
vector<int> inorder = {4, 2, 5, 1, 6, 3};
TreeNode* root = buildTree(preorder, inorder, 0, preorder.size() - 1, 0, inorder.size() - 1);
vector<int> levelResult = levelOrder(root);
cout << "The level order traversal of the binary tree is: ";
for (int val : levelResult) {
cout << val << " ";
}
cout << endl;
int targetVal;
cout << "Please enter the value of the node you want to search: ";
cin >> targetVal;
unordered_map<TreeNode*, TreeNode*> parent; // 记录节点的父节点
vector<TreeNode*> path = searchNode(root, targetVal, parent);
if (path.empty()) {
cout << "The node is not found!" << endl;
} else {
cout << "The path to the node is: ";
for (TreeNode* node : path) {
cout << node->val << " ";
}
cout << endl;
}
return 0;
}
```
输出结果为:
```
The level order traversal of the binary tree is: 1 2 3 4 5 6
Please enter the value of the node you want to search: 5
The path to the node is: 1 2 5
```
其中,buildTree函数采用递归的方式根据先序遍历和中序遍历构建二叉树。levelOrder函数采用队列实现二叉树的层序遍历。searchNode函数采用广度优先搜索实现节点的查找,并使用unordered_map记录每个节点的父节点,从而获取节点的路径。在主函数中,我们输入先序遍历和中序遍历,构建二叉树,然后输出二叉树的层序遍历。接着,我们输入一个节点的值,调用searchNode函数搜索该节点,并输出路径。
阅读全文