2025年8月16日 星期六

[145] Binary Tree Postorder Traversal

復健題.......怎麼那麼感人, 修完compile error 就過了....
(痛哭流涕)

**
 * 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().
 */
void postorder(struct TreeNode* root ,int *idx, int* ret){
    if (root == NULL)
        return;
    postorder(root->left, idx, ret);
    postorder(root->right, idx, ret);
    ret[(*idx)++]=root->val;
//printf(" [%d %d]", *idx, root->val);
    return;
}


int* postorderTraversal(struct TreeNode* root, int* returnSize) {
//    if (root == NULL)
//        return;
    int count =0;
    int *ret = calloc(100, sizeof(int));
    postorder(root , &count , ret);
    *returnSize = count;
    return ret;
}

沒有留言:

張貼留言