[Leetcode 104]二叉树最大深度Maximum Depth of Binary Tree
题目
求二叉树的深度,即根节点出发的最长路径上点的个数,即最长路径+1(本身这个点
https://leetcode.com/problems/maximum-depth-of-binary-tree/
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.
Example 1:

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
思路
DFS思路参考最长直径https://leetcode.com/problems/diameter-of-binary-tree/
这里只用找到一边,不用相加,所以是max(left,right)
代码
class Solution {
public int maxDepth(TreeNode root) {
int ans=dfs(root,0);
return ans;
}
public int dfs(TreeNode root,int ans){
if(root==null)
return 0;
int left=dfs(root.left,ans);
int right=dfs(root.right,ans);
ans+=Math.max(left,right);
return ans+1;
}
}
[Leetcode 104]二叉树最大深度Maximum Depth of Binary Tree的更多相关文章
- LeetCode 104. 二叉树的最大深度(Maximum Depth of Binary Tree)
104. 二叉树的最大深度 104. Maximum Depth of Binary Tree 题目描述 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说 ...
- [Swift]LeetCode104. 二叉树的最大深度 | Maximum Depth of Binary Tree
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- leetcode 104 Maximum Depth of Binary Tree二叉树求深度
Maximum Depth of Binary Tree Total Accepted: 63668 Total Submissions: 141121 My Submissions Question ...
- LeetCode Javascript实现 258. Add Digits 104. Maximum Depth of Binary Tree 226. Invert Binary Tree
258. Add Digits Digit root 数根问题 /** * @param {number} num * @return {number} */ var addDigits = func ...
- [LintCode] Maximum Depth of Binary Tree 二叉树的最大深度
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- LeetCode 104. Maximum Depth of Binary Tree C++ 解题报告
104. Maximum Depth of Binary Tree -- Easy 方法 使用递归 /** * Definition for a binary tree node. * struct ...
- 【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. Maximum Depth of Binary Tree(C++)
104. Maximum Depth of Binary Tree Given a binary tree, find its maximum depth. The maximum depth is ...
- 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练习题】Maximum Depth of Binary Tree
Maximum Depth of Binary Tree Given a binary tree, find its maximum depth. The maximum depth is the n ...
随机推荐
- Ubuntu16.04系统语言设置为中文以及搜狗输入法的安装
特别声明:本文是在操作完才做的记录,不是特别详细,见谅哈! 虚拟机安装的Ubuntu16.04结果语言设置只有英文...起初没啥影响,后来发现自己的脚本注释显示全乱码,而且直接影响脚本运行(其实可能是 ...
- 城壁 (Rampart)
题意简述 给定一张 $H \times W $ 的网格图,其中有 \(P\) 个被标记的点,求边长为 \(L\) 或以上的正方形的个数,要求正方形的边不得经过被标记的点. \(1 \le H,W \l ...
- LaTex【五】latex导入中文包,支持中文显示
\documentclass{article} \usepackage{CJKutf8} \begin{document} \begin{CJK}{UTF8}{gbsn} 中文样例,UTF-8编码,字 ...
- java使用array是copyof创建新长度数组
Arrays.copyof() int[] copied = Arrays.copyOf(arr, 10); //10 the the length of the new array System.o ...
- 全新BPMN常用图形标识一览表
BPMN符号与BPMN的关系 BPMN,即业务流程建模与标记(手法),表达此类手法的即是业务流程模型示意图.业务流程示意图是由一系列的元素符号构成.作为BPMN的基础,这些符号将业务流程建模简单化.图 ...
- Guava Retry重试机制
1.添加pom依赖 <dependency> <groupId>com.github.rholder</groupId> <artifactId>gua ...
- Django 之 ORM1
1.ORM简介 MVC或者MVC框架中包括一个重要的部分,就是ORM,它实现了数据模型与数据库的解耦,即数据模型的设计不需要依赖于特定的数据库,通过简单的配置就可以轻松更换数据库,这极大的减轻了开发人 ...
- Jenkins+Appium+Pytest+Allure集成
前提: 已经部署好了Jenkins环境,包括工具配置等 我的环境: Jenkins服务由安装在虚拟机上的Docker启动 Appium相关运行环境安装在虚拟机所在的主机上windows 方式:在Jen ...
- JS篇(009)-javascript 对象的几种创建方式
答案: 第一种:Object 构造函数创建 var Person = new Object(); Person.name = "Nike"; Person.age = 29; 这行 ...
- React工程化实践之UI组件库
分享日期: 2022-11-08 分享内容: 组件不是 React 特有的概念,但是 React 将组件化的思想发扬光大,可谓用到了极致.良好的组件设计会是良好的应用开发基础,这一讲就让我们谈一谈Re ...