代码随想录算法训练营第14天 | 144. 二叉树的前序遍历 | 145. 二叉树的后序遍历 | 94. 二叉树的中序遍历

144. 二叉树的前序遍历

解1: 递归

void preorder(struct TreeNode* root, int *array, int *idx) {
    if (root != NULL) {
        array[(*idx)++] = root->val;
        preorder(root->left, array, idx);
        preorder(root->right, array, idx);
    }
}


int* preorderTraversal(struct TreeNode* root, int* returnSize) {
    int *array = (int *)malloc(sizeof(int) * 100);

    memset(array, 0, sizeof(int) * 100);

    *returnSize = 0;
    preorder(root, array, returnSize);

    return array;
}

解2: 迭代

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

struct stack{
    int top;
    struct TreeNode *stk[100];
};

int* preorderTraversal(struct TreeNode* root, int* returnSize) {
    int *array = (int *)malloc(sizeof(int) * 100);
    struct stack *st = (struct stack *)malloc(sizeof(*st));
    struct TreeNode *node = root;

    memset(st, 0, sizeof(*st));
    st->top = -1;
    *returnSize = 0;

    if (root == NULL) {
        return array;
    }

    while (st->top > -1 || node != NULL) {
        while (node != NULL) {
            array[(*returnSize)++] = node->val;
            st->stk[++st->top] = node;
            node = node->left;
        }

        node = st->stk[st->top--];
        node = node->right;
    }

    printf("%d\n", *returnSize);

    return array;
}

解3:Morris 遍历

/**
 * 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* preorderTraversal(struct TreeNode* root, int* returnSize) {
    int *array = (int *)malloc(sizeof(int) * 100);
    struct TreeNode *cur = root, *pre = NULL;

    *returnSize = 0;

    if (root == NULL) {
        return array;
    }

    while (cur != NULL) {
        pre = cur->left;
        if (pre != NULL) {
            while (pre->right != NULL && pre->right != cur) {
                pre = pre->right;
            }  

            if (pre->right == NULL) {
                array[(*returnSize)++] = cur->val;
                pre->right = cur;
                cur = cur->left;
                continue;
            } else {
                pre->right = NULL;
            }
        } else {
            array[(*returnSize)++] = cur->val;
        }

        cur = cur->right;
    }

    return array;
}

145. 二叉树的后序遍历

解1: 递归

void postorder(struct TreeNode *root, int *ans, int *returnSize) {
    if (root != NULL) {
        postorder(root->left, ans, returnSize);
        postorder(root->right, ans, returnSize);
        ans[(*returnSize)++] = root->val;
    }
}

int* postorderTraversal(struct TreeNode* root, int* returnSize) {
    int *ans = (int *)malloc(sizeof(int) * 100);

    *returnSize = 0;

    postorder(root, ans, returnSize);

    return ans;
}

解2: 迭代

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

struct stack{
    int top;
    struct TreeNode *stk[100];
};

void reverse(int *array, int n) {
    int left = 0, right = n-1;
    int tmp;

    while (left < right) {
        tmp = array[left];
        array[left] = array[right];
        array[right] = tmp;
        left++, right--;
    }
}

int* postorderTraversal(struct TreeNode* root, int* returnSize) {
    int *array = (int *)malloc(sizeof(int) * 100);
    struct stack *st = (struct stack *)malloc(sizeof(*st));
    struct TreeNode *node = NULL;

    memset(st, 0, sizeof(*st));
    st->top = -1;
    *returnSize = 0;

    if (root == NULL) {
        return array;
    }

    st->stk[++st->top] = root;

    while (st->top > -1) {
        node = st->stk[st->top--];
        array[(*returnSize)++] = node->val;

        if (node->left) st->stk[++st->top] = node->left;
        if (node->right) st->stk[++st->top] = node->right;
    }

    reverse(array, *returnSize);

    return array;
}

解3:Morris 遍历

94. 二叉树的中序遍历

解1: 递归

void inorder(struct TreeNode* root, int *array, int *idx) {
    if (root != NULL) {
        inorder(root->left, array, idx);
        array[(*idx)++] = root->val;
        inorder(root->right, array, idx);
    }
}


int* inorderTraversal(struct TreeNode* root, int* returnSize) {
    int *array = (int *)malloc(sizeof(int) * 100);

    memset(array, 0, sizeof(int) * 100);

    *returnSize = 0;
    inorder(root, array, returnSize);

    return array;
}

解2: 迭代

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

struct stack{
    int top;
    struct TreeNode *stk[100];
};


int* inorderTraversal(struct TreeNode* root, int* returnSize) {
    int *array = (int *)malloc(sizeof(int) * 100);
    struct stack *st = (struct stack *)malloc(sizeof(*st));
    struct TreeNode *node = NULL;

    memset(st, 0, sizeof(*st));
    st->top = -1;
    *returnSize = 0;

    if (root == NULL) {
        return array;
    }

    node = root;

    while (st->top > -1 || node != NULL) {
        while (node != NULL) {
            st->stk[++st->top] = node;
            node = node->left;
        }

        node = st->stk[st->top--];
        array[(*returnSize)++] = node->val;
        node = node->right;
    }

    return array;
}

解3:Morris 遍历

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值