145. Binary Tree Postorder Traversal

145. Binary Tree Postorder Traversal

1. 题目

Given the root of a binary tree, return the postorder traversal of its nodes’ values.

Follow up: Recursive solution is trivial, could you do it iteratively?

Example1:

img

Input: root = [1,null,2,3]
Output: [3,2,1]

Constraints:

  • The number of the nodes in the tree is in the range [0, 100].
  • -100 <= Node.val <= 100

2. 解法

2.1 递归法
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */

void postOrder(struct TreeNode* root, int* returnSize, int *result)
{
    if(NULL == root){
        return ;
    }
    postOrder(root->left, returnSize, result);
    postOrder(root->right, returnSize, result);

    result[(*returnSize)++] = root->val;
}



/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* postorderTraversal(struct TreeNode* root, int* returnSize){
    *returnSize = 0;

    int *result = (int *)malloc(sizeof(int) * 1024);
    if(NULL == result){
        return NULL;
    }

    postOrder(root, returnSize, result);
    
    return result;
}
2.2 迭代法之砍右子树
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
void postOrder_Stack (struct TreeNode* root, int* returnSize, int *result)
{
    if(NULL == root){
        return ;
    }
    *returnSize = 0;
    int top = 0;

    struct TreeNode **stack = (struct TreeNode **)malloc(sizeof(struct TreeNode *) * 1024);
    if(NULL == stack){
        return ;
    }
    struct TreeNode *successor=NULL;

    while(root || top > 0){
        while(root){
            stack[top++] = root;
            root = root->left;
        }
        root = stack[--top];

        if(NULL == root->right){
            result[(*returnSize)++] = root->val;
            root = root->right;
        }else{
            successor = root;
            stack[top++] = root;
            root = root->right;
            successor->right = NULL;
        }

    }
}

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* postorderTraversal(struct TreeNode* root, int* returnSize){
    *returnSize = 0;

    int *result = (int *)malloc(sizeof(int) * 1024);
    if(NULL == result){
        return NULL;
    }

    postOrder_Stack(root, returnSize, result);
    
    return result;
}

2.3 迭代法之标记已处理节点

个点入栈后再加入一个空指针来标记此节点右子树已经遍历。

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
void postOrder_Stack_2 (struct TreeNode* root, int* returnSize, int *result)
{
    if(NULL == root){
        return ;
    }
    *returnSize = 0;
    int top = 0;

    struct TreeNode **stack = (struct TreeNode **)malloc(sizeof(struct TreeNode *) * 1024);
    if(NULL == stack){
        return ;
    }
    struct TreeNode *successor=NULL;

    while(root || top > 0){
        while(root){
            stack[top++] = root;
            root = root->left;
        }
        root = stack[--top];

        if(NULL == root){
            root = stack[--top];
            result[(*returnSize)++] = root->val;
            root = NULL;
        }else if(NULL == root->right){
            result[(*returnSize)++] = root->val;
            root = root->right;
        }else{
            successor = root;
            stack[top++] = root;
            stack[top++] = NULL;
            root = root->right;
            successor->right = NULL;
        }

    }
}


/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* postorderTraversal(struct TreeNode* root, int* returnSize){
    *returnSize = 0;

    int *result = (int *)malloc(sizeof(int) * 1024);
    if(NULL == result){
        return NULL;
    }

    postOrder_Stack_2(root, returnSize, result);
    
    return result;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

叨陪鲤

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值