LeetCode Maximum Depth of Binary Tree
Given a binary tree, find its maximum depth.
The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
题意:求二叉树从根到最远叶子结点的距离
思路:用DFS
/*** Definition for binary tree* public class TreeNode {* int val;* TreeNode left;* TreeNode right;* TreeNode(int x) { val = x; }* }*/
public class Solution {public int maxDepth(TreeNode root) {if (root == null) return 0;else if (root.left == null && root.right == null) return 1;else {int l = 0;int r = 0;if (root.left != null) l = 1 + maxDepth(root.left);if (root.right != null) r = 1 + maxDepth(root.right);return Math.max(l, r);}}
}
LeetCode Maximum Depth of Binary Tree相关推荐
- LeetCode——Maximum Depth of Binary Tree
LeetCode--Maximum Depth of Binary Tree Question Given a binary tree, find its maximum depth. The max ...
- Leetcode: Maximum Depth of Binary Tree
题目:算出二叉树的最大深度 解决方案:(1)BFS (2)DFS (1)BFS 一层一层往下搜索,一直找到最深的点,这里由于节点的val是没有用的,所以可以用来存储当前节点的深度,然后注意指针一定要初 ...
- LeetCode - Maximum Depth of Binary Tree
递归求二叉树的最大深度. /*** Definition for binary tree* public class TreeNode {* int val;* TreeNode left;* Tre ...
- LeetCode:Minimum Depth of Binary Tree,Maximum Depth of Binary Tree
LeetCode:Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth ...
- 【LeetCode 剑指offer刷题】树题4:104 Maximum Depth of Binary Tree
[LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) 104. Maximum Depth of Binary Tree Given a binary tree, fin ...
- Leetcode | Minimum/Maximum Depth of Binary Tree
Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...
- 【LeetCode】104. Maximum Depth of Binary Tree (2 solutions)
Maximum Depth of Binary Tree Given a binary tree, find its maximum depth. The maximum depth is the ...
- 领扣-104/111 二叉树的最大深度 Maximum Depth of Binary Tree MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- LC 104. Maximum Depth of Binary Tree
1.题意 104. Maximum Depth of Binary Tree Easy 98540 Given a binary tree, find its maximum depth. The m ...
最新文章
- log4net环境配置
- 《华尔街日报》专访雷军:小米如何应对挑战者身份的变化
- 如何在Elasticsearch中进行深分页
- Job 失败了怎么办?- 每天5分钟玩转 Docker 容器技术(133)
- decimal double java_Java BigDecimal和double BigDecimal类
- 网站服务器睡眠后还能访问吗,远程服务器可以睡眠吗
- 【java】java 如何证明linux缓存行确实存在
- 功能详细设计文档模板
- 工作中使用到的单词(软件开发)_2022_0815备份
- 公告栏模板php代码,[免插件]为wordpress主题怎样添加滚动公告栏功能
- Linux 脚本修改ps1,Linux使环境变量PS1的修改永久生效——修改配置文件/etc/profile...
- 宝塔远程桌面助手使用教程
- 常见地图坐标系以及转换方法、转换工具
- 真无线蓝牙耳机哪家的音质最好?四款音质好蓝牙耳机
- Python 条件语句
- 随机效应估算与固定效应估算_固定效应模型的估计原理说明.pdf
- 内网服务器安装docker
- k5b计算机联锁驱动故障,计算机联锁系统设备维护计算机联锁系统设备维护DS6-K5B计算机联锁系统采集与驱动电路故障查找.docx...
- 【数据结构与算法】【14】以树状形式打印二叉树
- 红芯电子对FPGA逻辑设计的解读
热门文章
- Visual Studio附加到进程调试
- 不是计算机专业学python能找到工作吗-非计算机行业为什么要转行学习Python编程...
- python常用模块大全总结-python常用模块整理
- python3.5怎么使用-Python3.5中NumPy模块的使用图文教程
- 爬虫python代码-python爬虫(附源码)
- python职能-python随机模块22个函数详解(下)
- 零基础学python 明日科技-明日科技 - 课程
- python爬虫百度百科-如何入门 Python 爬虫?
- java和python哪个好就业2020-python和java自学哪个好?
- 学python有哪些书推荐-有什么Python学习的书籍和学习资源推荐?