首页 > 编程语言 > 详细

每日LeetCode - 145. 二叉树的后序遍历(C语言)

时间:2021-06-16 22:00:21      阅读:22      评论:0      收藏:0      [点我收藏+]

技术分享图片

 

C语言

#define NULL ((void *)0)
/**
 * 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().
 */
int* postorderTraversal(struct TreeNode* root, int* returnSize){
    int* res = (int*)malloc(sizeof(res)*501);
    *returnSize=0;
    postorder(root, res, returnSize);
    return res;
}

void postorder(struct TreeNode* root, int*res, int* resSize){
    if (!root)
        return;

    postorder(root->left, res, resSize);
    postorder(root->right, res, resSize);
    res[(*resSize)++]=root->val;
}

 

每日LeetCode - 145. 二叉树的后序遍历(C语言)

原文:https://www.cnblogs.com/vicky2021/p/14890473.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!