鱼C论坛

 找回密码
 立即注册
查看: 487|回复: 1

[已解决]先序序列创建二叉树,输出先序序列、中序序列、后序序列并输出叶子结点数

[复制链接]
发表于 2024-11-15 16:10:23 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
对于给定的二叉树,输出其先序序列、中序序列、后序序列并输出叶子结点数。

二叉树.png

输入格式:
二叉树的先序遍历序列。

提示:一棵二叉树的先序序列是一个字符串,若字符是‘#’,表示该二叉树是空树,否则该字符是相应结点的数据元素。

输出格式:
若是非空二叉树,则输出四行内容 第一行是二叉树的先序遍历序列; 第二行是二叉树的中序遍历序列; 第三行是二叉树的后序遍历序列; 第四行是叶子结点数;

若是空二叉树 只需输出叶子数0

输入样例1:
FCA##DB###EHM###G##
输出样例1:
FCADBEHMG
ACBDFMHEG
ABDCMHGEF
4
输入样例2:
#
输出样例2:
0

这题用C或者C++怎么做
最佳答案
2024-11-15 16:10:38
要解决这个问题,我们可以使用递归来构建二叉树,然后对生成的树进行先序、中序和后序遍历。此外,我们需要计算二叉树的叶子结点数。下面是一个简单的C++代码示例来实现这个功能:

  1. #include <iostream>
  2. #include <string>

  3. using namespace std;

  4. // 二叉树结点的定义
  5. struct TreeNode {
  6.     char data;
  7.     TreeNode* left;
  8.     TreeNode* right;
  9.     TreeNode(char val) : data(val), left(nullptr), right(nullptr) {}
  10. };

  11. // 利用先序序列递归创建二叉树
  12. TreeNode* createTree(string& preorder, int& index) {
  13.     if (index >= preorder.size() || preorder[index] == '#') {
  14.         index++;
  15.         return nullptr;
  16.     }
  17.     TreeNode* node = new TreeNode(preorder[index++]);
  18.     node->left = createTree(preorder, index);
  19.     node->right = createTree(preorder, index);
  20.     return node;
  21. }

  22. // 先序遍历
  23. void preorderTraversal(TreeNode* root, string& result) {
  24.     if (root == nullptr) return;
  25.     result += root->data;
  26.     preorderTraversal(root->left, result);
  27.     preorderTraversal(root->right, result);
  28. }

  29. // 中序遍历
  30. void inorderTraversal(TreeNode* root, string& result) {
  31.     if (root == nullptr) return;
  32.     inorderTraversal(root->left, result);
  33.     result += root->data;
  34.     inorderTraversal(root->right, result);
  35. }

  36. // 后序遍历
  37. void postorderTraversal(TreeNode* root, string& result) {
  38.     if (root == nullptr) return;
  39.     postorderTraversal(root->left, result);
  40.     postorderTraversal(root->right, result);
  41.     result += root->data;
  42. }

  43. // 计算叶子结点数
  44. int countLeaves(TreeNode* root) {
  45.     if (root == nullptr) return 0;
  46.     if (root->left == nullptr && root->right == nullptr) return 1;
  47.     return countLeaves(root->left) + countLeaves(root->right);
  48. }

  49. int main() {
  50.     string preorder;
  51.     cin >> preorder;
  52.    
  53.     int index = 0;
  54.     TreeNode* root = createTree(preorder, index);
  55.    
  56.     if (!root) {
  57.         cout << "0" << endl;
  58.         return 0;
  59.     }
  60.    
  61.     string preOrderResult, inOrderResult, postOrderResult;
  62.     preorderTraversal(root, preOrderResult);
  63.     inorderTraversal(root, inOrderResult);
  64.     postorderTraversal(root, postOrderResult);
  65.    
  66.     int leafCount = countLeaves(root);
  67.    
  68.     cout << preOrderResult << endl;
  69.     cout << inOrderResult << endl;
  70.     cout << postOrderResult << endl;
  71.     cout << leafCount << endl;
  72.    
  73.     return 0;
  74. }
复制代码


### 程序说明:
1. **`createTree`函数**:从给定的先序序列构建二叉树。
2. **遍历函数**:分别实现了先序、中序和后序遍历。
3. **`countLeaves`函数**:递归计算叶子结点的数量。
4. **`main`函数**:读取输入,构建树,进行遍历并输出结果。

在实际使用时,将样例输入传递给程序,它将按照要求输出二叉树的遍历结果和叶子结点数。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2024-11-15 16:10:38 | 显示全部楼层    本楼为最佳答案   
要解决这个问题,我们可以使用递归来构建二叉树,然后对生成的树进行先序、中序和后序遍历。此外,我们需要计算二叉树的叶子结点数。下面是一个简单的C++代码示例来实现这个功能:

  1. #include <iostream>
  2. #include <string>

  3. using namespace std;

  4. // 二叉树结点的定义
  5. struct TreeNode {
  6.     char data;
  7.     TreeNode* left;
  8.     TreeNode* right;
  9.     TreeNode(char val) : data(val), left(nullptr), right(nullptr) {}
  10. };

  11. // 利用先序序列递归创建二叉树
  12. TreeNode* createTree(string& preorder, int& index) {
  13.     if (index >= preorder.size() || preorder[index] == '#') {
  14.         index++;
  15.         return nullptr;
  16.     }
  17.     TreeNode* node = new TreeNode(preorder[index++]);
  18.     node->left = createTree(preorder, index);
  19.     node->right = createTree(preorder, index);
  20.     return node;
  21. }

  22. // 先序遍历
  23. void preorderTraversal(TreeNode* root, string& result) {
  24.     if (root == nullptr) return;
  25.     result += root->data;
  26.     preorderTraversal(root->left, result);
  27.     preorderTraversal(root->right, result);
  28. }

  29. // 中序遍历
  30. void inorderTraversal(TreeNode* root, string& result) {
  31.     if (root == nullptr) return;
  32.     inorderTraversal(root->left, result);
  33.     result += root->data;
  34.     inorderTraversal(root->right, result);
  35. }

  36. // 后序遍历
  37. void postorderTraversal(TreeNode* root, string& result) {
  38.     if (root == nullptr) return;
  39.     postorderTraversal(root->left, result);
  40.     postorderTraversal(root->right, result);
  41.     result += root->data;
  42. }

  43. // 计算叶子结点数
  44. int countLeaves(TreeNode* root) {
  45.     if (root == nullptr) return 0;
  46.     if (root->left == nullptr && root->right == nullptr) return 1;
  47.     return countLeaves(root->left) + countLeaves(root->right);
  48. }

  49. int main() {
  50.     string preorder;
  51.     cin >> preorder;
  52.    
  53.     int index = 0;
  54.     TreeNode* root = createTree(preorder, index);
  55.    
  56.     if (!root) {
  57.         cout << "0" << endl;
  58.         return 0;
  59.     }
  60.    
  61.     string preOrderResult, inOrderResult, postOrderResult;
  62.     preorderTraversal(root, preOrderResult);
  63.     inorderTraversal(root, inOrderResult);
  64.     postorderTraversal(root, postOrderResult);
  65.    
  66.     int leafCount = countLeaves(root);
  67.    
  68.     cout << preOrderResult << endl;
  69.     cout << inOrderResult << endl;
  70.     cout << postOrderResult << endl;
  71.     cout << leafCount << endl;
  72.    
  73.     return 0;
  74. }
复制代码


### 程序说明:
1. **`createTree`函数**:从给定的先序序列构建二叉树。
2. **遍历函数**:分别实现了先序、中序和后序遍历。
3. **`countLeaves`函数**:递归计算叶子结点的数量。
4. **`main`函数**:读取输入,构建树,进行遍历并输出结果。

在实际使用时,将样例输入传递给程序,它将按照要求输出二叉树的遍历结果和叶子结点数。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-10 01:09

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表