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

题目

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


数据结构与定义

#include 
#include 

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

二叉树形状

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


核心代码

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);
        }
    }
}

核心代码快照

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


全部代码

#include 
#include 

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;
}

结束语

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

你可能感兴趣的:(算法与数据结构,C/C++,算法,数据结构)