C++编写程序,输入先序和中序遍历,构建二叉树,给出该二叉树的层序遍历, 并输入一个节点的值,完成在该树中搜索该节点,并输出其路径。
时间: 2024-03-24 12:37:13 浏览: 61
好的,这是一个比较复杂的问题,我会尽力回答。
首先,我们需要了解二叉树的先序遍历、中序遍历和层序遍历的定义:
- 先序遍历:按照根节点 -> 左子树 -> 右子树的顺序访问每个节点。
- 中序遍历:按照左子树 -> 根节点 -> 右子树的顺序访问每个节点。
- 层序遍历:按照从上到下、从左到右的顺序访问每个节点。
根据先序遍历和中序遍历,我们可以构建出原二叉树。具体步骤如下:
1. 将先序遍历和中序遍历分别存储在两个数组中。
2. 根据先序遍历数组的第一个元素确定根节点。
3. 在中序遍历数组中找到根节点的位置,将中序遍历数组分成左子树和右子树两个部分。
4. 根据左子树和右子树的长度,将先序遍历数组分成左子树和右子树两个部分。
5. 递归地构建左子树和右子树。
下面是 C++ 代码实现:
```cpp
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
public:
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
unordered_map<int, int> inorder_map;
for (int i = 0; i < inorder.size(); i++) {
inorder_map[inorder[i]] = i;
}
return buildTreeHelper(preorder, inorder, inorder_map, 0, preorder.size() - 1, 0, inorder.size() - 1);
}
TreeNode* buildTreeHelper(vector<int>& preorder, vector<int>& inorder, unordered_map<int, int>& inorder_map, int pre_left, int pre_right, int in_left, int in_right) {
if (pre_left > pre_right || in_left > in_right) {
return NULL;
}
int root_val = preorder[pre_left];
int root_index = inorder_map[root_val];
int left_size = root_index - in_left;
TreeNode* root = new TreeNode(root_val);
root->left = buildTreeHelper(preorder, inorder, inorder_map, pre_left + 1, pre_left + left_size, in_left, root_index - 1);
root->right = buildTreeHelper(preorder, inorder, inorder_map, pre_left + left_size + 1, pre_right, root_index + 1, in_right);
return root;
}
vector<vector<int>> levelOrder(TreeNode* root) {
vector<vector<int>> res;
if (!root) {
return res;
}
queue<TreeNode*> q;
q.push(root);
while (!q.empty()) {
int size = q.size();
vector<int> level;
for (int i = 0; i < size; i++) {
TreeNode* node = q.front();
q.pop();
level.push_back(node->val);
if (node->left) {
q.push(node->left);
}
if (node->right) {
q.push(node->right);
}
}
res.push_back(level);
}
return res;
}
vector<int> search(TreeNode* root, int val) {
vector<int> path;
searchHelper(root, val, path);
return path;
}
bool searchHelper(TreeNode* root, int val, vector<int>& path) {
if (!root) {
return false;
}
path.push_back(root->val);
if (root->val == val) {
return true;
}
if (searchHelper(root->left, val, path) || searchHelper(root->right, val, path)) {
return true;
}
path.pop_back();
return false;
}
};
int main() {
vector<int> preorder = {3, 9, 20, 15, 7};
vector<int> inorder = {9, 3, 15, 20, 7};
Solution s;
TreeNode* root = s.buildTree(preorder, inorder);
vector<vector<int>> level_order = s.levelOrder(root);
for (int i = 0; i < level_order.size(); i++) {
for (int j = 0; j < level_order[i].size(); j++) {
cout << level_order[i][j] << " ";
}
cout << endl;
}
int target = 15;
vector<int> path = s.search(root, target);
if (path.empty()) {
cout << "Target " << target << " not found!" << endl;
} else {
cout << "Path to target " << target << ": ";
for (int i = 0; i < path.size(); i++) {
cout << path[i] << " ";
}
cout << endl;
}
return 0;
}
```
输出结果为:
```
3
9 20
15 7
Path to target 15: 3 20 15
```
这里我们先构建了一棵二叉树,然后输出了它的层序遍历。接着,我们搜索了值为 15 的节点,并输出了到该节点的路径。
阅读全文