【数据结构与算法】二叉树中从每个叶子结点到根结点的路径

题目

  Qestion:输出二叉树中从每个叶子结点到根结点的路径


数据结构与定义

#include <stdio.h>
#include <stdlib.h>

typedef struct TreeNode
{
    int val;
    struct TreeNode *left;
    struct TreeNode *right;
} TreeNode;

二叉树形状

在这里插入图片描述


核心代码

void LeafToRoot(TreeNode *node, int length, int *Path)
{
    // 结点不存在
    if (node == NULL)
        return;
    // 结点存在
    else
    {
        Path[length] = node->val;
        length = length + 1;
        // 该结点为叶子结点
        if (node->left == NULL && node->right == NULL)
        {
            // 输出路径上每个结点的值
            for (int i = 0; i < length; i++)
            {
                printf("%d ", Path[i]);
            }
            printf("\n");
        }
        else
        {
            LeafToRoot(node->left, length, Path);
            LeafToRoot(node->right, length, Path);
        }
    }
}

核心代码快照

在这里插入图片描述


全部代码

#include <stdio.h>
#include <stdlib.h>

typedef struct TreeNode
{
    int val;
    struct TreeNode *left;
    struct TreeNode *right;
} TreeNode;

void inputTree(TreeNode *root)
{
    root->val = 1;
    root->left = (TreeNode *)malloc(sizeof(TreeNode));
    root->left->val = 2;
    root->left->left = (TreeNode *)malloc(sizeof(TreeNode));
    root->left->right = (TreeNode *)malloc(sizeof(TreeNode));
    root->left->left->val = 4;
    root->left->left->left = NULL;
    root->left->left->right = NULL;
    root->left->right->val = 5;
    root->left->right->left = NULL;
    root->left->right->right = NULL;

    root->right = (TreeNode *)malloc(sizeof(TreeNode));
    root->right->val = 3;
    root->right->left = (TreeNode *)malloc(sizeof(TreeNode));
    root->right->left->val = 6;
    root->right->left->left = (TreeNode *)malloc(sizeof(TreeNode));
    root->right->left->left->val = 8;
    root->right->left->left->left = NULL;
    root->right->left->left->right = NULL;
    root->right->left->right = NULL;
    root->right->right = (TreeNode *)malloc(sizeof(TreeNode));
    root->right->right->val = 7;
    root->right->right->left = NULL;
    root->right->right->right = NULL;
}

void LeafToRoot(TreeNode *node, int length, int *Path)
{
    // 结点不存在
    if (node == NULL)
        return;
    // 结点存在
    else
    {
        Path[length] = node->val;
        length = length + 1;
        // 该结点为叶子结点
        if (node->left == NULL && node->right == NULL)
        {
            // 输出路径上每个结点的值
            for (int i = 0; i < length; i++)
            {
                printf("%d ", Path[i]);
            }
            printf("\n");
        }
        else
        {
            LeafToRoot(node->left, length, Path);
            LeafToRoot(node->right, length, Path);
        }
    }
}
int main()
{
    int Path[20];
    int length = 0;
    TreeNode *root = new TreeNode;
    inputTree(root);
    LeafToRoot(root, length, Path);
    return 0;
}

结束语

  因为是算法小菜,所以提供的方法和思路可能不是很好,请多多包涵~如果有疑问欢迎大家留言讨论,你如果觉得这篇文章对你有帮助可以给我一个免费的赞吗?我们之间的交流是我最大的动力!

  • 14
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hiddenSharp429

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值