题目

求二叉树的深度,即根节点出发的最长路径上点的个数,即最长路径+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的更多相关文章

  1. LeetCode 104. 二叉树的最大深度(Maximum Depth of Binary Tree)

    104. 二叉树的最大深度 104. Maximum Depth of Binary Tree 题目描述 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说 ...

  2. [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 ...

  3. leetcode 104 Maximum Depth of Binary Tree二叉树求深度

    Maximum Depth of Binary Tree Total Accepted: 63668 Total Submissions: 141121 My Submissions Question ...

  4. 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 ...

  5. [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 ...

  6. LeetCode 104. Maximum Depth of Binary Tree C++ 解题报告

    104. Maximum Depth of Binary Tree -- Easy 方法 使用递归 /** * Definition for a binary tree node. * struct ...

  7. 【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 ...

  8. 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 ...

  9. 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 ...

  10. 【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 ...

随机推荐

  1. 【OBS Studio】使用 VLC 视频源播放视频报错:Unhandled exception: c0000005

    使用 OBS Studio 和 VLC media player 可以实现视频播放列表的推流,参考『OBS如何添加播放列表?』. 但是使用过程中发现使用 VLC 视频源播放视频时,一个视频播放完切换下 ...

  2. pySpark RDD基本用法

    pySpark RDD基本用法 RDD的全称是:Resilient Distributed Dataset (弹性分布式数据集),它有几个关键的特性: RDD是只读的,表示它的不可变性. 可以并行的操 ...

  3. Paddiing 组件

    一.Flutter Paddiing 组件 在 html 中常见的布局标签都有 padding 属性,但是 Flutter 中很多 Widget 是没有 padding 属性.这个时候我们可以用 Pa ...

  4. 摹客RP,编辑界面缩放比例支持手动输入!

    Hello,小伙伴们,又到了摹客的新功能播报时间. 本月更新,摹客RP编辑界面缩放比例支持手动输入,并对部分组件的默认样式及属性进行了优化:摹客DT率先上线了3款黄金比例图层,辅助设计师更高效绘图:针 ...

  5. Matchmaker Server 像素流送配对服务器

  6. Decal Buffer相关

    延迟渲染与前向渲染 前向渲染或叫正向渲染,每一个图元都经过顶点着色器,图元着色器,片段着色器,在片段着色器内连同光照一起计算,效率和图元数量有关. 延迟渲染会先计算出G-BUFFER,就是不含光照计算 ...

  7. laravel 导入导出(实际上还是php代码实现)

    1.导出 public function excel(){        $data=User::all();        $data=json_decode(json_encode($data), ...

  8. hashMap 获取里面value最大的值得key

    public static void main(String[] args) { Map<String, Integer> map = new HashMap(); map.put(&qu ...

  9. Safari高级使用

    Safari是苹果公司为旗下设备开发的一款强大的浏览器不论是iPhone还是iPad亦或是MAC OS上都能使用.但是针对不同的系统,Safari也有一定的改动.那么在MacOS中如何使用Safari ...

  10. vue 和react 不同之我见

    1数据是不是可变的 2通过js操作一切还是各自的处理方式 react的思路是all in js,通过js来生成html,所以设计了jsx,还有通过js来操作css,社区的styled-componen ...