Leetcode 145. 二叉树的后序遍历(DAY 7 - Easy.Vision)(DAY 9神级Morris)

原题题目

Leetcode 145. 二叉树的后序遍历(DAY 7 - Easy.Vision)(DAY 9神级Morris)_第1张图片



代码实现(首刷自解)

/**
 * 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().
 */
#define MAX 1001

void visit(struct TreeNode* root,int* returnSize,int* arr)
{
     
    if(root)
    {
     
        visit(root->left,returnSize,arr);
        visit(root->right,returnSize,arr);
        arr[(*returnSize)++] = root->val;
    }
}

int* postorderTraversal(struct TreeNode* root, int* returnSize){
     
    int* arr = (int*)malloc(sizeof(struct TreeNode) * MAX);
    (*returnSize) = 0;
    visit(root,returnSize,arr);
    return arr;
}


代码实现(二刷Morris)(抄的代码太难理解了)

void addPath(int *vec, int *vecSize, struct TreeNode *node) {
     
    int count = 0;
    while (node != NULL) {
     
        ++count;
        vec[(*vecSize)++] = node->val;
        node = node->right;
    }
    for (int i = (*vecSize) - count, j = (*vecSize) - 1; i < j; ++i, --j) {
     
        int t = vec[i];
        vec[i] = vec[j];
        vec[j] = t;
    }
}

int *postorderTraversal(struct TreeNode *root, int *returnSize) {
     
    int *res = malloc(sizeof(int) * 2001);
    *returnSize = 0;
    if (root == NULL) {
     
        return res;
    }

    struct TreeNode *p1 = root, *p2 = NULL;

    while (p1 != NULL) {
     
        p2 = p1->left;
        if (p2 != NULL) {
     
            while (p2->right != NULL && p2->right != p1) {
     
                p2 = p2->right;
            }
            if (p2->right == NULL) {
     
                p2->right = p1;
                p1 = p1->left;
                continue;
            } else {
     
                p2->right = NULL;
                addPath(res, returnSize, p1->left);
            }
        }
        p1 = p1->right;
    }
    addPath(res, returnSize, root);
    return res;
}


代码实现(迭代 纯迭代也挺难理解的 就用了reverse 借助一个数组+一个栈)(前序遍历把先访问左子数边为右子树 然后翻转)

/**
 * 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().
 */

#define MAX 2001
int *postorderTraversal(struct TreeNode *root, int *returnSize) {
     
    int *res = malloc(sizeof(int) * MAX),size = 0;
    *returnSize = 0;
    int arr[MAX] = {
     0};
    struct TreeNode* stack[MAX],*position = root;
    while(position != NULL || size > 0)
    {
     
        while(position != NULL)
        {
     
            stack[size++] = position;
            arr[(*returnSize)++] = position->val;       
            position = position->right;          
        }
        position = stack[--size];
        position = position->left;
    }
    int i = *returnSize - 1,j;
    for(j=0;j < *returnSize ;)
        res[j++] = arr[i--];
    return res;
}

你可能感兴趣的:(#,从c开始的进步之路,c语言)