算法:Maximum Depth of Binary Tree(二叉树的最大深度)
说明
算法:Maximum Depth of Binary Tree
LeetCode地址:https://leetcode.com/problems/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.
Note: A leaf is a node with no children.
Example:
Given binary tree [3,9,20,null,null,15,7],
3/ \9 20/ \15 7
return its depth = 3.
解题思路
求二叉树的深度,实际上是求左右子树中最大的深度,递归思想可求出.
深搜即可,每深入一次节点加一即可,然后取左右子树的最大深度。
代码实现
/*** Definition for a binary tree node.* public class TreeNode {* int val;* TreeNode left;* TreeNode right;* TreeNode(int x) { val = x; }* }*/
class Solution {public int maxDepth(TreeNode root) {if (root == null) {return 0;}return 1 + Math.max(maxDepth(root.left), maxDepth(root.right));}
}
代码执行效率
Runtime: 0 ms, faster than 100.00% of Java online submissions for Maximum Depth of Binary Tree.
Memory Usage: 40.2 MB, less than 5.01% of Java online submissions for Maximum Depth of Binary Tree.
总结
求二叉树的深度问题,实则考察递归的应用。
代码下载:
https://github.com/zgpeace/awesome-java-leetcode/blob/master/code/LeetCode/src/MaximumDepthOfBinaryTree.java
算法:Maximum Depth of Binary Tree(二叉树的最大深度)相关推荐
- 104. Maximum Depth of Binary Tree 二叉树的最大深度
给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说明: 叶子节点是指没有子节点的节点. 示例: 给定二叉树 [3,9,20,null,null,15,7], ...
- LeetCode:104_Maximum Depth of Binary Tree | 二叉树的最大深度 | Easy
要求:求二叉树的深度(二叉树的深度为最远叶子节点到根节点的距离,即根节点到最远叶子节点的距离) Given a binary tree, find its maximum depth.The maxi ...
- 领扣-104/111 二叉树的最大深度 Maximum Depth of Binary Tree MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- 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 | 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 ...
- 【LeetCode 剑指offer刷题】树题4:104 Maximum Depth of Binary Tree
[LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) 104. Maximum Depth of Binary Tree Given a binary tree, fin ...
- LeetCode——Maximum Depth of Binary Tree
LeetCode--Maximum Depth of Binary Tree Question Given a binary tree, find its maximum depth. The max ...
- 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 ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
最新文章
- 电脑启动后黑屏只有鼠标_电脑加装固态硬盘后出现黑屏或蓝屏解决方案
- linux-----强大的find
- 【HDU - 3038】How Many Answers Are Wrong (带权并查集--权为区间和)
- c++程序设计中文件输入输出流知识点
- 显卡风扇不转电脑黑屏_隔壁妹子电脑坏了怎么办,简单易懂电脑维修方法与技巧收藏备用...
- Nginx之http配置
- php实现开关效果代码,JavaScript实现开关效果的代码分享
- MySQL 5.6 for Windows 解压缩版配置安装(转)
- 多功能PCIE交换机之七:单NT到双NT
- 5-9 第五天 微信 JS-SDK-使用微信官方的WEB调试工具
- gevent实现套接字
- 随手记---字符和词汇的区别
- avast android高级版,排名第一 Android杀毒软件avast!评测
- 芝诺数解|「十一」千里姻缘一“线”牵—重庆网络婚恋分析报告
- 如何将DVD的vob视频格式转换成mp4格式
- 5G时代下的室内定位方案越来越精准-室内定位方案-新导智能
- 洗礼灵魂,修炼python(56)--爬虫篇—知识补充—编码之url编码
- 安徽省软考报名时间成绩查询安徽省教育考试院安徽省人事考试网报名入口
- 这可是全网EVE安装最完整,最详细的图解,没有之一【安装图解】
- linux系统(Centos 7)部署环境记录(显卡驱动、CUDA、CuDnn和conda环境安装)
热门文章
- mysql中的concat和group_concat的用法
- 完整的vue-cli3项目创建过程以及各种配置
- CSS兼容IE6,IE7和FF的总结
- 记一次2048小游戏开发
- phpstorm常用功能快捷键(mac)
- 【权值分块】bzoj1588 [HNOI2002]营业额统计
- linux磁盘分配最佳实践,干货再次来袭!Linux小白最佳实践:《超容易的Linux系统管理入门书》(连载八)用命令实现批量添加用户...
- android4.4.2 以太网代理,Android2.3.4系统添加Ethernet框架支持
- java 云 代码_我 - java代码库 - 云代码
- y空间兑换代码_如何让Python代码加速运行?