257. 二叉树的所有路径-C语言

给定一个二叉树,返回所有从根节点到叶子节点的路径。

说明: 叶子节点是指没有子节点的节点。

示例:

输入:

   1
 /   \
2     3
 \
  5

输出: ["1->2->5", "1->3"]

解释: 所有根节点到叶子节点的路径为: 1->2->5, 1->3

 

void EveryPath(struct TreeNode *root,char **bin_trees,int *path,int depth,int *returnSize)
{
    int i;
    if (root ==NULL)
        return ;
    if (root->left ==NULL && root->right ==NULL){
        path[depth]=root->val;
        bin_trees[*returnSize] =(char *)calloc(1,sizeof(char *) *100);
        for(i=0;i<depth ;i++)
        {
            sprintf(bin_trees[*returnSize],"%s%d->",bin_trees[*returnSize],path[i]);
        }
        sprintf(bin_trees[*returnSize],"%s%d",bin_trees[*returnSize],path[i]);
        *returnSize+=1;
    }else{
        path[depth++]=root->val;//节点开始回溯时,这里依然保存着原先的值
        EveryPath(root->left,bin_trees,path,depth,returnSize);
        EveryPath(root->right,bin_trees,path,depth,returnSize);
    }
}
char ** binaryTreePaths(struct TreeNode* root, int* returnSize){  
    *returnSize =0;
    int path[100]={0};
    char **bin_trees =(char **)malloc(sizeof(char *) *100);
    if (root==NULL)
        return NULL;
    EveryPath(root,bin_trees,path,0,returnSize);
    return bin_trees;
}

这道题的问题在于输入[]时,提交时报错了

执行出错信息:Line 209: Char 3: runtime error: load of null pointer of type 'char *' (__Serializer__.c)

错误的原因

需要将*returnSize 设置为0 在返回。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是求解二叉树所有路径的C语言代码: ```c #include <stdio.h> #include <stdlib.h> struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; }; void printPaths(struct TreeNode* root, int path[], int pathLen) { if (!root) return; path[pathLen++] = root->val; if (!root->left && !root->right) { for (int i = 0; i < pathLen; i++) { printf("%d ", path[i]); } printf("\n"); } else { printPaths(root->left, path, pathLen); printPaths(root->right, path, pathLen); } } int main() { struct TreeNode* root = (struct TreeNode*)malloc(sizeof(struct TreeNode)); root->val = 1; root->left = (struct TreeNode*)malloc(sizeof(struct TreeNode)); root->left->val = 2; root->left->left = NULL; root->left->right = NULL; root->right = (struct TreeNode*)malloc(sizeof(struct TreeNode)); root->right->val = 3; root->right->left = (struct TreeNode*)malloc(sizeof(struct TreeNode)); root->right->left->val = 4; root->right->left->left = NULL; root->right->left->right = NULL; root->right->right = (struct TreeNode*)malloc(sizeof(struct TreeNode)); root->right->right->val = 5; root->right->right->left = NULL; root->right->right->right = NULL; int path[100]; printPaths(root, path, 0); return 0; } ``` 这段代码实现了二叉树所有路径的打印,其中 `printPaths` 函数采用递归实现,每次递归时将当前节点的值加入到 `path` 数组中,并判断当前节点是否为叶子节点,若为叶子节点,则打印 `path` 数组中的所有值,否则继续递归遍历其左右子节点。 `main` 函数中构建了一个二叉树并调用 `printPaths` 函数进行遍历打印。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值