当前位置:网站首页>剑指 Offer 55 - I. 二叉树的深度-dfs法
剑指 Offer 55 - I. 二叉树的深度-dfs法
2022-06-24 07:07:00 【Mr Gao】
剑指 Offer 55 - I. 二叉树的深度
输入一棵二叉树的根节点,求该树的深度。从根节点到叶节点依次经过的节点(含根、叶节点)形成树的一条路径,最长路径的长度为树的深度。
例如:
给定二叉树 [3,9,20,null,null,15,7],
3
/
9 20
/
15 7
返回它的最大深度 3 。
这题很常规,但是编程中也很实用,可以学习学习:
/** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */
int f(struct TreeNode* root){
if(root){
int a=f(root->left)+1;
int b=f(root->right)+1;
if(a>b){
return a;
}
else{
return b;
}
}
else{
return 0;
}
}
int maxDepth(struct TreeNode* root){
return f(root);
}
边栏推荐
猜你喜欢

一文讲透,商业智能BI未来发展趋势如何

every()、map()、forEarch()方法。数组里面有对象的情况

Qt 中发送自定义事件

New technology practice, encapsulating the permission application library step by step with the activity results API

一文详解|增长那些事儿

Centos7安装jdk8以及mysql5.7以及Navicat连接虚拟机mysql的出错以及解决方法(附mysql下载出错解决办法)

2022.06.23(LC_144,94,145_二叉树的前序、中序、后序遍历)

Pymysql inserts data into MySQL and reports an error for no reason

疫情、失业,2022,我们高喊着摆烂和躺平!

Send custom events in QT
随机推荐
xargs使用技巧 —— 筑梦之路
One development skill a day: how to establish P2P communication based on webrtc?
数据中台:民生银行的数据中台实践方案
Base64编码详解及其变种(解决加号在URL变空格问题)
偶然间得到的framework工具类 自用
[force deduction 10 days SQL introduction] Day3
Qt源码分析--QObject(2)
rpiplay实现树莓派AirPlay投屏器
Shell basic operator -- arithmetic operator
数据库,查询本月借出书的数量,如果高于10本时,显示“本月借出书大于10本”,否则显示“本月借出书小于10本”
Why do you want to file? What are the advantages and disadvantages of website filing?
Win11 blank when using VIM to view content in cmder
Earthly 容器镜像构建工具 —— 筑梦之路
Redis cluster data skew
关于ETL看这篇文章就够了,三分钟让你明白什么是ETL
Ordering of MySQL composite index
every()、map()、forEarch()方法。数组里面有对象的情况
Redis的Cluster集群数据倾斜
Building a static website with eleventy
原生小程序用画布制作海报,等比例缩放,和uniapp差不多就是写法有点不同