题号: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.
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心得
我想离职了,有没有公司缺人啊
前一篇说到 该怎麽写 data 的资料,找回双向绑定机制 !!! 这边先小小的补充一下 XD 在 O...
嗨咪纳桑,咱是immortalmice,今天要来和各位分享自己研究出的几个新资料结构 这个资料结构支...
最後要介绍的是在Golang中比较特别的struct/method/interface struct...
App 混淆再资安保护领域来说,可谓是最复杂的一环,也是最重要的一环 但是资安检测无法有个标准的检...
布局排板大板型左右留边 Container 接下来就是这个Container里头需要载运那些内容了 ...