数据结构--力扣144.二叉树的前序遍历(C

链接:. - 力扣(LeetCode)【点击即可跳转】

使用c语言来实现。

代码展示为:

复制代码
/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
 int TreeSize( struct TreeNode* root)
{
    return root==NULL?0:TreeSize(root->left)+TreeSize(root->right)+1;
}
void _prevOrder( struct TreeNode*root,int* a,int* pi)
{
    if(root==NULL)
    {
        return;
    }
    a[*pi]=root->val;
    ++(*pi);
    _prevOrder(root->left,a,pi);
    _prevOrder(root->right,a,pi);
}

int* preorderTraversal( struct TreeNode* root, int* returnSize)
{
    int size=TreeSize(root);
    int* a=(int*)malloc(size*sizeof(int));
    int i=0;
    _prevOrder(root,a,&i);

    *returnSize=size;
    return a;
}
相关推荐
寒秋花开曾相惜8 小时前
(学习笔记)第四章 处理器体系结构
linux·网络·数据结构·笔记·学习
疏星浅月8 小时前
虚拟内存三大核心作用详解
linux·c语言·arm开发·嵌入式硬件
故事和你918 小时前
洛谷-数据结构1-1-线性表1
开发语言·数据结构·c++·算法·leetcode·动态规划·图论
脱氧核糖核酸__8 小时前
LeetCode热题100——53.最大子数组和(题解+答案+要点)
数据结构·c++·算法·leetcode
脱氧核糖核酸__9 小时前
LeetCode 热题100——42.接雨水(题目+题解+答案)
数据结构·c++·算法·leetcode
王老师青少年编程10 小时前
csp信奥赛C++高频考点专项训练之贪心算法 --【线性扫描贪心】:数列分段 Section I
c++·算法·编程·贪心·csp·信奥赛·线性扫描贪心
王老师青少年编程10 小时前
csp信奥赛C++高频考点专项训练之贪心算法 --【线性扫描贪心】:分糖果
c++·算法·贪心算法·csp·信奥赛·线性扫描贪心·分糖果
_日拱一卒10 小时前
LeetCode:2两数相加
算法·leetcode·职场和发展
py有趣10 小时前
力扣热门100题之零钱兑换
算法·leetcode
董董灿是个攻城狮10 小时前
Opus 4.7 来了,我并不建议你升级
算法