首页 > 其他分享 >257. 二叉树的所有路径c

257. 二叉树的所有路径c

时间:2024-03-05 20:47:57浏览次数:18  
标签:temp int top 路径 257 char 二叉树 root stack

很好的题目,让我的sprintf旋转

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
/**
 * Note: The returned array must be malloced, assume caller calls free().
 */

char* change(int* stack,int top){
    char* t=(char*)malloc(sizeof(char)*100);
    int index=0;
    for(int i=0;i<top-1;i++){
        index+=sprintf(t+index,"%d->",stack[i]);
    }
    sprintf(t+index,"%d",stack[top-1]);
    return t;
}
  
void preorder(struct TreeNode* root,char** temp,int *returnSize,int* stack,int top){
    if(!root) return;
    if(!root->left&&!root->right){
        stack[top++]=root->val;
        char* x=change(stack,top);
        temp[(*returnSize)++]=x;
    }else{
        stack[top++]=root->val;
        preorder(root->left,temp,returnSize,stack,top);
        preorder(root->right,temp,returnSize,stack,top);
    }
}

char** binaryTreePaths(struct TreeNode* root, int* returnSize) {
    *returnSize=0;   
    if(!root) return NULL;
    char** temp=(char**)malloc(sizeof(char*)*100);
    int stack[100]={0};
    int top=0;
    preorder(root,temp,returnSize,stack,top);
    return temp;
}

结果:

标签:temp,int,top,路径,257,char,二叉树,root,stack
From: https://www.cnblogs.com/llllmz/p/18054886

相关文章

  • 110. 平衡二叉树c
    /***Definitionforabinarytreenode.*structTreeNode{*intval;*structTreeNode*left;*structTreeNode*right;*};*/intmax(inti,intj){if(i>j)returni;returnj;}intheight(structTreeNode*root){......
  • 代码随想录算法训练营第三十六天| ● 738.单调递增的数字 ● 968.监控二叉树 ● 总
    单调递增的数字 题目链接:738.单调递增的数字-力扣(LeetCode)思路:从左向右验证是否按位单调递增,如果出现递减的区间,则从该位开始验证该位减1后是否比左边的相邻位大,如果不符合就接着向左寻找这样的位,如果找到了,则将该位前面的位复制到结果中,该位减1加入结果,该位之后的位全部改......
  • 111. 二叉树的最小深度c
    /***Definitionforabinarytreenode.*structTreeNode{*intval;*structTreeNode*left;*structTreeNode*right;*};*/intmin(inti,intj){if(i<j)returni;returnj;}intminDepth(structTreeNode*root){......
  • 222. 完全二叉树的节点个数c
    /***Definitionforabinarytreenode.*structTreeNode{*intval;*structTreeNode*left;*structTreeNode*right;*};*/intcountNodes(structTreeNode*root){if(!root)return0;if(!root->left&&!root->......
  • 226. 翻转二叉树 c
    层次遍历的题目C写吐血了,缓一缓再写那种气死人的题目。/***Definitionforabinarytreenode.*structTreeNode{*intval;*structTreeNode*left;*structTreeNode*right;*};*/voidreverseroot(structTreeNode*root){if(!root)......
  • [数据结构] 树、森林及二叉树的应用
    树、森林树的存储结构双亲表示法双亲表示法的存储结构#defineMAX_TREE_SIZE100typedefstruct{intdata;intparent;}PTNode;typedefstruct{PTNodenodes[MAX_TREE_SIZE];intn;}PTree;【注】区别树的顺序存储结构与二叉树的顺序存储结......
  • Windows定时删除指定路径下N天前的文件以及文件夹
    Windows定时删除指定路径下N天前的文件以及文件夹 将下面代码复制到自建的.txt文件里,然后保存再更改后辍名为.bat,点击执行即可脚本文件存放随意,因为里面的路径是绝对路径1.删除指定路径下5天前的所有文件@echooffsetSrcDir=E:\WORK\GitsetDaysAgo=5forfiles/p......
  • VS2022 Android 虚拟操作系统的路径更改-设备管理器
     默认新增加的虚拟操作系统在C盘      path=C:\Users\HP\.android\avd\pixel_5_-_api_34.avd 更改后F:\pixel_5_-_api_34.avd ......
  • python-pip更改下载路径,解决超时问题
    有时pip安装包时,会提示pip._vendor.urllib3.exceptions.ReadTimeoutError:HTTPSConnectionPool(host='files.pythonhosted.org',port=443):Readtimedout.原因跟解决方式PyPI镜像:考虑使用PyPI的镜像站点。中国用户经常遇到与files.pythonhosted.org的连接问题,因此他们经常......
  • [数据结构] 树与二叉树
    树的基本概念树的定义树是由\(n(n\geq0)\)个节点组成的有限集。当\(n=0\)时,称为空树。任意一棵非空树应满足以下两点:(1)有且仅有一个特定的称为根的节点;(2)当\(n>1\)时,其余节点可分为\(m(m>0)\)个互不相交的有限集\(T_1,T_2,\dots,T_m\),其中每个集合本身又是一棵树,称为......