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:
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;
}

浙公网安备 33010602011771号