【数据结构与算法】求任意二叉树中第一条最长的路径长度,并输出此路径上各结点的值

题目

  Qestion:求任意二叉树中第一条最长的路径长度,并输出此路径上各结点的值。


数据结构与定义

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

二叉树形状

在这里插入图片描述


核心代码

void FindLongesetPath(TreeNode *node, int length, int *MaxLength, int *currentPath, int *longestPath)
{
    // 该节点不存在
    if (node == NULL)
        return;
    // 该结点存在
    else
    {
        // 先记录当前结点的值,随后长度加一
        currentPath[length] = node->val;
        length = length + 1;
        // 已经到达叶子节点
        if (node->left == NULL && node->right == NULL)
        {
            if (*MaxLength < length)
            {
                *MaxLength = length;
                for (int i = 0; i < *MaxLength; i++) // 更新最长路径
                {
                    longestPath[i] = currentPath[i];
                }
            }
            return;
        }
        // 还有子结点
        else
        {
            FindLongesetPath(node->left, length, MaxLength, currentPath, longestPath);
            FindLongesetPath(node->right, length, MaxLength, currentPath, longestPath);
        }
    }
}

核心代码快照

在这里插入图片描述


全部代码(可运行)

#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 FindLongesetPath(TreeNode *node, int length, int *MaxLength, int *currentPath, int *longestPath)
{
    // 该节点不存在
    if (node == NULL)
        return;
    // 该结点存在
    else
    {
        // 先记录当前结点的值,随后长度加一
        currentPath[length] = node->val;
        length = length + 1;
        // 已经到达叶子节点
        if (node->left == NULL && node->right == NULL)
        {
            if (*MaxLength < length)
            {
                *MaxLength = length;
                for (int i = 0; i < *MaxLength; i++) // 更新最长路径
                {
                    longestPath[i] = currentPath[i];
                }
            }
            return;
        }
        // 还有子结点
        else
        {
            FindLongesetPath(node->left, length, MaxLength, currentPath, longestPath);
            FindLongesetPath(node->right, length, MaxLength, currentPath, longestPath);
        }
    }
}

int main()
{
    int maxLength = 0;
    int Length = 0;
    int currentPath[20]; // 当前路径
    int longestPath[20]; // 最长路径
    TreeNode *root = (TreeNode *)malloc(sizeof(TreeNode));
    // 输入二叉树
    inputTree(root);
    FindLongesetPath(root, Length, &maxLength, currentPath, longestPath); // 查找第一个最长的路径
    printf("第一个最长路径上的结点为:");
    for (int i = 0; i < maxLength; i++)
    {
        printf("%d ", longestPath[i]);
    }
    return 0;
}

结束语

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

  • 14
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

hiddenSharp429

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

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

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

打赏作者

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

抵扣说明:

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

余额充值