zoukankan      html  css  js  c++  java
  • leetcode递归学习

    leetcode递归学习

    参考:
    [1]https://www.jianshu.com/p/1395fae8a1ae

    三步走

    (1)确定终止条件
    (2)确定递归过程
    (3)确定本层递归返回值

    一、104 二叉树最大深度

    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     struct TreeNode *left;
     *     struct TreeNode *right;
     * };
     */
    
    // 递归三步走:确定大出口条件,确定递归过程,确定当前层递归出口
    int maxDepth(struct TreeNode* root){
        // 最终出口条件
        if (root == NULL) {
            return 0;
        }
        //递归过程
        int leftDepth = maxDepth(root->left);
        int rightDepth = maxDepth(root->right);
        //本层返回值
        return (leftDepth > rightDepth) ? (leftDepth + 1) : (rightDepth + 1);
    }
    
    

    二、24两两交换链表中的节点

    实际上只要能处理单个过程就可以搞定问题,思维方式的强大力量

    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     struct ListNode *next;
     * };
     */
    
    // 递归三步走:确定终止条件,确定递归过程,确定本层出口
    struct ListNode* swapPairs(struct ListNode* head){
        // 1、终止条件
        if (head == NULL || head->next == NULL) {
            return head;
        }
    
        // 2、递归过程:head、head->next、已经处理ok的swapPairs( head)
        struct ListNode* nextTemp = head->next;
        // 递归过程解析:nextTemp->next为待处理部分,整个流程,实际上是从前往后处理
        head->next = swapPairs(nextTemp->next);
        // 完成 head 与 head->next的交换
        nextTemp->next = head;
    
        // 3、本层出口:交换之后,链表的表头为nextTemp;
        return nextTemp;
    }
    
    

    三、110. 平衡二叉树

    未调试通过

    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     struct TreeNode *left;
     *     struct TreeNode *right;
     * };
     */
    #include <math.h>
    // 返回值:该节点的深度、该节点是否为平衡、节点
    struct NodeStatue {
        int depth;
        bool isBal;
    };
    
    struct NodeStatue isBalancedTree(struct TreeNode* treeNode)
    {
        // 1、终止条件:叶子节点
        struct NodeStatue nodeStatue;
        if (treeNode = NULL) {
            nodeStatue.depth = 0;
            nodeStatue.isBal = true;
            return nodeStatue;
        }
        // 2、递归过程
        struct NodeStatue leftNodeStatue;
        struct NodeStatue rightNodeStatue;
        leftNodeStatue = isBalancedTree(treeNode->left);
        rightNodeStatue = isBalancedTree(treeNode->right);
        // 3、本次递归出口
        // 条件1:如果子树不平衡,则大树平衡
        if (leftNodeStatue.isBal == false || rightNodeStatue.isBal == false) {
            nodeStatue.depth = 0;
            nodeStatue.isBal = false;
            return nodeStatue;
        }
        // 条件2:左右子树高度差
        if (abs(leftNodeStatue.depth - rightNodeStatue.depth) > 1) {
            nodeStatue.depth = 0;
            nodeStatue.isBal = false;
            return nodeStatue;
        }
        // 平衡树处理
        nodeStatue.depth = ((leftNodeStatue.depth >  rightNodeStatue.depth) ? 
                             leftNodeStatue.depth : rightNodeStatue.depth) + 1;
        nodeStatue.isBal = true;
        return nodeStatue;
    }
    // 递归三步走:确定终止条件、确定递归过程、确定本层出口
    bool isBalanced(struct TreeNode* root){
        // 因为输入输出不对口,需要其他函数处理
        return isBalancedTree(root).isBal;
    
    }
    
    
  • 相关阅读:
    C#+API实现指定窗体激活
    DEVC++学习之(一)
    javascript 实现原生下载的各种情况
    IssueVision 之WebService安全篇
    Add relationship to BS sample
    ExpandRelationWithCtxt 与 GetRelatedObjects 的区别
    C#调用javascript
    解禁网页限制
    Unix cc options vs gcc options
    IssueVision 之模式篇
  • 原文地址:https://www.cnblogs.com/HZL2017/p/13277251.html
Copyright © 2011-2022 走看看