void creatPath(struct TreeNode*root,char **path,int *nodes,int *returnSize,int top){
if(root!=NULL){
if(root->left==NULL&&root->right==NULL){
char *nodes_path=malloc(101*sizeof(char));
int len=0;
for(int i=0;i<top;i++){
len+=sprintf(nodes_path+len,"%d->",nodes[i]);
}
sprintf(nodes_path+len,"%d",root->val);
path[(*returnSize)++]=nodes_path;
}
else{
nodes[top++]=root->val;
creatPath(root->left,path,nodes,returnSize,top);
creatPath(root->right,path,nodes,returnSize,top);
}
}
}
char ** binaryTreePaths(struct TreeNode* root, int* returnSize){
char**paths=malloc(101*sizeof(char*));
*returnSize=0;
int nodes[200];
int top=0;
creatPath(root,paths,nodes,returnSize,top);
return paths;
}
Leetcode第257题二叉树的所有路径||C语言(待更新)
最新推荐文章于 2024-09-21 14:48:47 发布