蓝桥杯 历届试题 横向打印二叉树

好久没写博客了...因为好久没刷题了QAQ...

还是先附上大佬博客Orz:

https://blog.csdn.net/survivorone/article/details/60871378

https://blog.csdn.net/qq_40103462/article/details/79527537

这道题还是蛮好的,但是我太菜了啊55555...大体思路就是,后序遍历,先记录从根到当前结点的完整字符串,再判断输出时是"."还是"|",最后输出本节点的数,如果是非叶子节点,再加上"-|"。

需要注意的是,改变node时需要加引用,因为它本身就是Node *类型;dfs函数中string s,p不能加引用,因为其记录的是从根到当前节点的路径而不是整个遍历过程的路径。如果感觉有点乱,就去掉注释看看中间过程吧。

还有其他的细节就不多说了,直接附上100%通过代码:

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<malloc.h>
using namespace std;
const int INF=0x3f3f3f3f;

const int MAX=10000*100+10;
typedef struct Node
{
    int val;//值
    char num[7];//数值对应的字符串
    int hei;//深度
    struct Node *lchild,*rchild;
    //char ans[MAX];//记录每个节点的答案
}Node;
Node *node=NULL;
int root;//树根

void change(char t[],int num)//将数值转化为字符串
{
    int j=0,k=num;
    while(k)
    {
        t[j++]=(k%10+'0');
        k/=10;
    }
    for(int i=0,ii=j-1;i<ii;i++,ii--)
        swap(t[i],t[ii]);
    t[j]='\0';
    //cout<<t<<endl;
}
//构建二叉树
void ins(Node * &t,int num,int height)//要加引用!!!
{
    if(t==NULL)//插入位置
    {
        t=(Node *)malloc(sizeof(Node));
        t->val=num;
        change(t->num,num);
        t->hei=height+1;
        //cout<<"t->val="<<t->val<<" t->hei="<<t->hei<<endl;
        t->lchild=t->rchild=NULL;
    }
    else if(t->val>num)
        ins(t->lchild,num,t->hei);
    else
        ins(t->rchild,num,t->hei);
}

//后序遍历
void Inorder_traversal(Node *t)
{
    if(t==NULL)
        return;
    Inorder_traversal(t->rchild);
    printf("num=%d,hei=%d\n",t->val,t->hei);
    Inorder_traversal(t->lchild);
}
void dfs(Node * &t,string s,string p)//s保存完整字符串,p保存从树根到当前节点左右移动的关系
{
    if(t->val==root)//根节点
        s+=t->num;
    else//其余节点
    {
        s+="-|-";
        s+=t->num;
    }
    if(t->rchild)
    {
        dfs(t->rchild,s,p+"1");
    }
    int len=s.length();
    //cout<<"val="<<t->val<<" s="<<s<<" len="<<len<<" p="<<p<<endl;
    len-=(strlen(t->num)+2);
    int cnt=1,lenp=p.length();
    for(int i=0;i<len;i++)
    {
        if(s[i]<='9'&&s[i]>='0')
            printf(".");
        else if(s[i]=='-') //-|-
            printf(".");
        else if(s[i]=='|')
        {
            //cout<<endl<<"cnt="<<cnt<<" lenp="<<lenp<<" p[cnt]="<<p[cnt]<<endl;
            if(cnt<lenp&&p[cnt]==p[cnt-1])//左右孩子关系
                printf(".");
            else
                printf("|");
            cnt++;//无论哪种情况都要++
        }
    }
    if(t->val==root)
        printf("%s",t->num);
    else
        printf("|-%s",t->num);
    if(t->lchild||t->rchild)//非叶子节点
        printf("-|");
    printf("\n");
    if(t->lchild)
    {
        dfs(t->lchild,s,p+"0");
    }
}

int main()
{
    int x;
    /*for(int i=1;i<=6;i++)
    {
        scanf("%d",&x);
        ins(node,x,-1);
    }*/
    while(scanf("%d",&x)==1)
    {
        ins(node,x,-1);
    }
    //Inorder_traversal(node);//后序遍历
    root=node->val;
    string s="";
    string p="";
    dfs(node,s,p);
	return 0;
}

/*
5 10 20 8 4 7
5 4 3 6 7
5 3 7 6 4

10 8 5 7 12 4
1 3 5 7 9 2 4 6 8

5 4 20 10 6 8
*/

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是实现二叉树按凹入表形式横向打印的C语言代码: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_DEPTH 100 typedef struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; } TreeNode; // 计算树的深度 int calcDepth(TreeNode *root) { if (root == NULL) { return 0; } int leftDepth = calcDepth(root->left); int rightDepth = calcDepth(root->right); return (leftDepth > rightDepth ? leftDepth : rightDepth) + 1; } // 按凹入表形式打印二叉树 void printTree(TreeNode *root) { if (root == NULL) { return; } int depth = calcDepth(root); int maxWidth = (1 << depth) - 1; char **res = (char **)malloc(sizeof(char *) * depth); for (int i = 0; i < depth; i++) { res[i] = (char *)malloc(sizeof(char) * (maxWidth + 1)); memset(res[i], ' ', maxWidth); res[i][maxWidth] = '\0'; } // 递归填充凹入表 void fillTable(TreeNode *node, int depth, int pos, int offset) { if (node == NULL) { return; } int idx = pos + offset; res[depth][idx] = node->val + '0'; fillTable(node->left, depth + 1, pos, offset / 2); fillTable(node->right, depth + 1, idx + 1, offset / 2); } fillTable(root, 0, 0, maxWidth / 2); // 打印凹入表 for (int i = 0; i < depth; i++) { printf("%s\n", res[i]); free(res[i]); } free(res); } // 创建二叉树 TreeNode *createTree(int *arr, int size, int pos) { if (pos >= size || arr[pos] == 0) { return NULL; } TreeNode *root = (TreeNode *)malloc(sizeof(TreeNode)); root->val = arr[pos]; root->left = createTree(arr, size, pos * 2 + 1); root->right = createTree(arr, size, pos * 2 + 2); return root; } int main() { int arr[] = {1, 2, 3, 0, 5, 6, 7}; // 示例二叉树 int size = sizeof(arr) / sizeof(arr[0]); TreeNode *root = createTree(arr, size, 0); printTree(root); // 按凹入表形式打印二叉树 return 0; } ``` 其中,`calcDepth` 函数计算树的深度,`printTree` 函数按凹入表形式打印二叉树,`fillTable` 函数递归填充凹入表。`createTree` 函数用于创建二叉树,这里使用了一个示例二叉树进行测试,你可以根据需要修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值