【C】二叉树打印

对于LeetCode练习时,有时需要直观的将树打印,提供如下demo供参考
输入
9
1,2,3,4,5,6,7,8,9,
效果
在这里插入图片描述

直接上代码

#include "stdio.h"
#include "string.h"

struct TreeNode {
    int val;
    struct TreeNode *left;
    struct TreeNode *right;
};
int *ArrIn(int *pArrSize);
struct TreeNode *TreeIn(int *arr, int arrSize, int curIndex);
void Display(struct TreeNode *root, int ident);

int main()
{
    int arrSize;
    int* arr = ArrIn(&arrSize);
    struct TreeNode *root = TreeIn(arr, arrSize, 0);
    Display(root, 0);

    return 0;
}

int *ArrIn(int *pArrSize)
{
    int arrSize;
    scanf("%d", &arrSize);
    printf("==================\n");
    int *arr = (int *)malloc(sizeof(int) * arrSize);
    for (int i = 0; i < arrSize; i++) {
        scanf("%d,", &arr[i]);
    }

    for (int  i = 0; i < arrSize; i++) {
        printf("%3d", arr[i]);
    }
    printf("\n==================\n");
    *pArrSize = arrSize;
    return arr;
}

void PreOrderTree(struct TreeNode *root) 
{
    if (root == NULL) {
        return;
    }
    printf("%d ", root->val);
    PreOrderTree(root->left);
    PreOrderTree(root->right);
    return;
}


int vec_left[100] = {0};
// 显示二叉树的函数,只要调用Display(root, 0)即可
void Display(struct TreeNode* root, int ident)
{
    if(ident > 0) {
        for(int i = 0; i < ident - 1; ++i) {
            if (vec_left[i]) {
                printf("%s", "|   ");
            } else {
                printf("%s", "    ");
            }  
        }
        printf("%s", "|-- ");
    }

    if(! root) {
        printf("(null)\n");
        return;
    }

    printf("%d\n", root->val);
    if(!root->left && !root->right) {
        return;
    }
    vec_left[ident] = 1;
    Display(root->left, ident + 1);
    vec_left[ident] = 0;
    Display(root->right, ident + 1);
}
struct TreeNode *TreeIn(int *arr, int arrSize, int curIndex)
{
    if (curIndex >= arrSize || arr[curIndex] == -1) {   // -1 表示空节点
        return NULL;
    }
    struct TreeNode *root = (struct TreeNode *)malloc(sizeof(struct TreeNode));
    root->val = arr[curIndex];
    root->left = TreeIn(arr, arrSize, curIndex * 2 + 1);
    root->right = TreeIn(arr, arrSize, curIndex * 2 + 2);
    return root;
}
  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是c语言实现二叉树镜像并打印的代码: ``` #include <stdio.h> #include <stdlib.h> //定义二叉树结构体 typedef struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; } TreeNode; //创建二叉树 TreeNode *createTree() { int val; scanf("%d", &val); if (val == -1) { return NULL; } TreeNode *node = (TreeNode *) malloc(sizeof(TreeNode)); node->val = val; node->left = createTree(); node->right = createTree(); return node; } //二叉树镜像 TreeNode *mirrorTree(TreeNode *root) { if (root == NULL) { return NULL; } TreeNode *temp = root->left; root->left = root->right; root->right = temp; mirrorTree(root->left); mirrorTree(root->right); return root; } //打印二叉树(中序遍历) void printTree(TreeNode *root) { if (root == NULL) { return; } printTree(root->left); printf("%d ", root->val); printTree(root->right); } int main() { printf("请输入二叉树的节点(-1代表空节点):\n"); TreeNode *root = createTree(); printf("二叉树镜像前的中序遍历:\n"); printTree(root); printf("\n"); TreeNode *newRoot = mirrorTree(root); printf("二叉树镜像后的中序遍历:\n"); printTree(newRoot); printf("\n"); return 0; } ``` 代码思路: 1. 定义二叉树结构体,包括节点值和左右子节点。 2. 创建二叉树函数,通过递归实现,如果输入的节点值为-1,则返回NULL,否则继续创建左右子节点。 3. 二叉树镜像函数,通过递归实现,首先交换当前节点的左右子节点,然后分别对左右子节点进行镜像操作。 4. 打印二叉树函数,通过中序遍历实现,先打印左子树,然后打印当前节点,最后打印右子树。 5. 在主函数中,先创建二叉树,然后打印二叉树镜像前的中序遍历,接着调用二叉树镜像函数,得到镜像后的二叉树,最后打印二叉树镜像后的中序遍历。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值