找LeetCode上简单的题目来撑过30天啦(DAY9)

题号:104 标题:Maximum Depth of Binary Tree难度:Easy

Given the root of a binary tree, return its maximum depth.

A binary tree's maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

Input: root = [3,9,20,null,null,15,7]
Output: 3

Example 2:
Input: root = [1,null,2]
Output: 2

Example 3:
Input: root = []
Output: 0

Example 4:
Input: root = [0]
Output: 1

Constraints:
The number of nodes in the tree is in the range [0, 104].
-100 <= Node.val <= 100

Time Limit Exceeded的程序码

int check(struct TreeNode* root2,int D2){
    if(root2 == NULL){
        return 0;
    }else if(root2->left != NULL || root2 -> right != NULL){
        D2= D2+1;    
        if(check(root2->left,D2) > check(root2->right,D2)){
            D2 = check(root2->left,D2);
        }else{
            D2 = check(root2->right,D2);
        }           
     }
    return D2;
}

int maxDepth(struct TreeNode* root){
    int D = 1;   
    return check(root,D);
}

最终的程序码

int check(struct TreeNode* root2,int D2){
    //printf("%d\n",root2->val);
    if(root2 == NULL){
        return 0;
    }else if(root2->left != NULL || root2 -> right != NULL){
        D2= D2+1;
        int l = check(root2->left,D2);
        int r  =  check(root2->right,D2);
        if(l > r){
            D2 =l;
        }else{
            D2 =r;
        }           
     }
    return D2;
}

int maxDepth(struct TreeNode* root){
    int D = 1;   
    return check(root,D);
}

花比较多的时间
今天花比较多时间在解决Time Limit Exceeded的问题,最後发现我在if里面recursive,导致不必要的call function,改为用变数存return的值再去做判断,就没有这个问题啦

DAY9心得
我想离职了,有没有公司缺人啊


<<:  C# .Net 使用 ADO.NET 连接资料库

>>:  [DAY-10] 人才密度最大化 留任测试

[前端暴龙机,Vue2.x 进化 Vue3 ] Day26. Vue3 Composition API 使用(二)

前一篇说到 该怎麽写 data 的资料,找回双向绑定机制 !!! 这边先小小的补充一下 XD 在 O...

咱研究出新的类阵列资料结构的说

嗨咪纳桑,咱是immortalmice,今天要来和各位分享自己研究出的几个新资料结构 这个资料结构支...

Golang快速入门-3(Day6)

最後要介绍的是在Golang中比较特别的struct/method/interface struct...

Day 30. 要别人看不懂,还是让自己看不懂的 - 混淆 Obfuscation

App 混淆再资安保护领域来说,可谓是最复杂的一环,也是最重要的一环 但是资安检测无法有个标准的检...

从 React 开始,让你的网页material-ui起来 [Day 4] 排版布局Grid

布局排板大板型左右留边 Container 接下来就是这个Container里头需要载运那些内容了 ...