C语言刷二叉树(一)基础部分

二叉树基础部分

144. 二叉树的前序遍历

方法一:递归

/**
 * 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 PreTra(struct TreeNode* curNode, int *arr, int *len)
{
    if (curNode == NULL) {
        return;
    }
    // 前序:中 左 右
    arr[(*len)++] = curNode->val;
    PreTra(curNode->left, arr, len);
    PreTra(curNode->right, arr, len);
}

int* preorderTraversal(struct TreeNode* root, int* returnSize)
{
    *returnSize = 0;
    int *res = (int *)malloc(sizeof(int) * 101);
    PreTra(root, res, returnSize);
    return res;
}

方法二:迭代(利用栈)

/**
 * 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)
{
    struct TreeNode *stack[10];
    int stackIndex = 0;
    if (root != NULL) {
        stack[stackIndex++] = root;
    }
    int *res = (int *)malloc(sizeof(int) * 101);
    *returnSize = 0;
    while (stackIndex != 0) {
        // 取出当前节点
        struct TreeNode *curNode = stack[stackIndex - 1];
        if (curNode == NULL) {
            break;
        }
        stackIndex--;
        res[(*returnSize)++] = curNode->val;
        // 先右节点
        if (curNode->right != NULL) {
            stack[stackIndex++] = curNode->right;
        }
        // 再左节点
        if (curNode->left != NULL) {
            stack[stackIndex++] = curNode->left;
        }
    }
    return res;
}

94. 二叉树的中序遍历

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

void InTra(struct TreeNode *curNode, int *arr, int *index)
{
    if (curNode == NULL) {
        return;
    }
    // 中序:左 中 右
    InTra(curNode->left, arr, index);
    arr[(*index)++] = curNode->val;
    InTra(curNode->right, arr, index);
}

int* inorderTraversal(struct TreeNode* root, int* returnSize)
{
    int *res = (int *)malloc(sizeof(int) * 101);
    *returnSize = 0;
    InTra(root, res, returnSize);
    return res;
}

方法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().
 */
int* inorderTraversal(struct TreeNode* root, int* returnSize)
{
    struct TreeNode *stack[10];
    int index = 0;

    int *res = (int *)malloc(sizeof(int) * 101);
    *returnSize = 0;
    struct TreeNode *curNode = root;

    while (curNode != NULL || index != 0) {
        // 到最底层叶子节点,去取左孩子
        if (curNode != NULL) {
            stack[index++] = curNode;
            curNode = curNode->left;
        } else { // 否则开始弹栈1次
            curNode = stack[--index];
            res[(*returnSize)++] = curNode->val;
            curNode = curNode->right;
        }
    }
    return res;
}

145. 二叉树的后序遍历

方法1:递归

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

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

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

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


void Swap(int *a, int *b)
{
    int tmp = *b;
    *b = *a;
    *a = tmp;
}

int* postorderTraversal(struct TreeNode* root, int* returnSize)
{
    struct TreeNode *stack[10];
    int index = 0;

    if (root != NULL) {
        stack[index++] = root;
    }

    int *res = (int *)malloc(sizeof(int) * 101);
    *returnSize = 0;

    while (index != 0) {
        struct TreeNode *curNode = stack[--index];
        res[(*returnSize)++] = curNode->val;


        if (curNode->left != NULL) {
            stack[index++] = curNode->left;
        }
        if (curNode->right != NULL) {
            stack[index++] = curNode->right;
        }
        
    }

    // 翻转
    int left = 0, right = *returnSize - 1;

    while (left < right) {
        Swap(&res[left], &res[right]);
        left++;
        right--;
    }

    return res;
}
原文地址:https://www.cnblogs.com/kongweisi/p/15797064.html