zoukankan      html  css  js  c++  java
  • c语言刷 DFS题记录

    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 Preorder(struct TreeNode* root, int* array, int* returnSize)
    {
        if (root == NULL) {
            return;
        }
        array[(*returnSize)++] = root->val;
        Preorder(root->left, array, returnSize);
        Preorder(root->right, array, returnSize);
    }
    
    int* preorderTraversal(struct TreeNode* root, int* returnSize)
    {
        int *res = (int *)malloc(sizeof(int) * 100);
        *returnSize = 0;
        Preorder(root, res, returnSize);
        return res;
    }

    145. 二叉树的后序遍历

    /**
     * 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 Postorder(struct TreeNode* root, int* array, int* returnSize)
    {
        if (root == NULL) {
            return;
        }
        Postorder(root->left, array, returnSize);
        Postorder(root->right, array, returnSize);
        array[(*returnSize)++] = root->val;
    }
    
    int* postorderTraversal(struct TreeNode* root, int* returnSize)
    {
        int *res = (int *)malloc(sizeof(int) * 100);
        *returnSize = 0;
        Postorder(root, res, returnSize);
        return res;
    }

    105. 从前序与中序遍历序列构造二叉树

    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     struct TreeNode *left;
     *     struct TreeNode *right;
     * };
     */
    
    struct TreeNode* buildTree(int* preorder, int preorderSize, int* inorder, int inorderSize)
    {
        if (preorder == NULL || inorder == NULL) {
            return NULL;
        } 
        if (preorderSize == 0 || inorderSize == 0) {
            return NULL;
        }
        if (preorderSize != inorderSize) {
            return NULL;
        }
    
        struct TreeNode *res = (struct TreeNode *)malloc(sizeof(struct TreeNode));
        int rootVal = preorder[0];
        int leftLen = 0;
        for (int i = 0; i < inorderSize; i++) {
            if (rootVal == inorder[i]) {
                leftLen = i;
                break;
            }
        }
        int rightLen = inorderSize - leftLen - 1;
    
        res->val = rootVal;
        res->left = buildTree(&preorder[1], leftLen, inorder, leftLen);
        res->right = buildTree(&preorder[leftLen + 1], rightLen, &inorder[leftLen + 1], rightLen);
        return res;
    }

    106. 从中序与后序遍历序列构造二叉树

    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     struct TreeNode *left;
     *     struct TreeNode *right;
     * };
     */
    
    
    struct TreeNode* buildTree(int* inorder, int inorderSize, int* postorder, int postorderSize)
    {
        if (inorder == NULL || postorder == NULL) {
            return NULL;  
        }
        if (inorderSize == 0 || postorderSize == 0) {
            return NULL;  
        }
        if (inorderSize != postorderSize) {
            return NULL;
        }
    
        struct TreeNode* res = (struct TreeNode *)malloc(sizeof(struct TreeNode));
        int rootVal = postorder[postorderSize - 1];
        int leftNum = 0;
        int rightNum = 0;
        for (int i = 0; i < inorderSize; i++) {
            if (rootVal == inorder[i]) {
                leftNum = i;
                break;
            }
        }
        rightNum = inorderSize - leftNum - 1;
        res->val = rootVal;
        res->left = buildTree(inorder, leftNum, postorder, leftNum);
        res->right = buildTree(&inorder[leftNum + 1], rightNum, &postorder[leftNum], rightNum);
        return res;
    }
  • 相关阅读:
    23种设计模式(转载)
    RabbitMQ JAVA客户端调用
    JavaScript中的this
    RedisDesktopManager 踩坑之旅
    webmagic使用手册
    Maven 手动添加本地jar包
    根据端口号查询 进程 并杀掉进程
    从经典面试题看java中类的加载机制
    Java线程的5种状态及切换(透彻讲解)
    JVM 类加载机制详解
  • 原文地址:https://www.cnblogs.com/kongweisi/p/15351526.html
Copyright © 2011-2022 走看看