树倒着打印输出

思路

先向右遍历,同时空格也要变多,那么就先prt(root->right,space+cnt) 其中space是离最左边多远,cnt是每次叠加的有多远

输出最右边端点 和 空行

再向左遍历

同样prt(root->left,space+cnt)

代码

#include <iostream>
#include <stack>
using namespace std;

typedef struct Node
{
    struct Node *left;
    struct Node *right;
    char data;
} N, *Tr;

void create(Tr *t)
{
    char ch;
    ch = getchar();
    if (ch == '.')
    {
        *t = NULL;
    }
    else
    {
        *t = (N *)malloc(sizeof(N));
        (*t)->data = ch;
        create(&(*t)->left);
        create(&(*t)->right);
    }
}
void printSpace(int num)
{
    for (int i = 0; i < num; i++)
    {
        cout << " ";
    }
}
void prt(Tr root, int space)
{
    if (root == NULL)
        return;
    int cnt = 1;
    prt(root->right, space + cnt);
    printSpace(space);
    cout << root->data << endl;
    prt(root->left, space + cnt);
}
int main()
{
    Tr Tree;
    create(&Tree);
    prt(Tree, 1);
    return 0;
}

相关推荐

  1. 打印输出

    2024-04-26 22:10:03       35 阅读
  2. 输入 wo xiang he ni jao peng you.,打。

    2024-04-26 22:10:03       44 阅读
  3. C# 打印输出以及文件输入输出

    2024-04-26 22:10:03       43 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-26 22:10:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-26 22:10:03       100 阅读
  3. 在Django里面运行非项目文件

    2024-04-26 22:10:03       82 阅读
  4. Python语言-面向对象

    2024-04-26 22:10:03       91 阅读

热门阅读

  1. ubuntu通过crontab创建定时任务,并执行sh

    2024-04-26 22:10:03       37 阅读
  2. 【QEMU系统分析之启动篇(十八)】

    2024-04-26 22:10:03       34 阅读
  3. 利用blob对象于浏览器保存图片到本地

    2024-04-26 22:10:03       35 阅读
  4. 可使用的 ESRGAN 超分模型

    2024-04-26 22:10:03       33 阅读
  5. c c++编程 fmt:占位符

    2024-04-26 22:10:03       30 阅读
  6. LC 202.快乐数

    2024-04-26 22:10:03       34 阅读
  7. c++ 智能指针 交换函数实验

    2024-04-26 22:10:03       31 阅读