题目

求二叉树的深度,即根节点出发的最长路径上点的个数,即最长路径+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. docker镜像原理(一)

    在理解什么是docker镜像之前我们需要搞懂虚拟机系统到底是怎么组成的如何实现的,docker中又是如何安装和使用虚拟机系统的,怎样可以高效灵活的切换系统发行版等问题 一.centos7系统长什么样 ...

  2. android studio 查看工程所有动画资源

  3. react 脚手架搭建项目 报错C:\Program Files\nodejs\node_cache\_logs\2022-12-28T14_38_28_286Z-debug-0.log

    报错内容: 解决方法: 第一步: 删除C:\Program Files\nodejs\node_cache\_logs 目录下所有文件 第二步:切换镜像 npm config set registry ...

  4. el-form不进行校验可能的原因

    可能原因 el-form至少需要:model="你的数据form",:rules='你的校验规则' 的属性: 只能校验el-input,不能对原始input进行校验 (大坑).

  5. python 识别登陆验证码图片(完整代码)

    在编写自动化测试用例的时候,每次登录都需要输入验证码,后来想把让python自己识别图片里的验证码,不需要自己手动登陆,所以查了一下识别功能怎么实现,做一下笔记. 首选导入一些用到的库,re.Imag ...

  6. 分布式事务 seata

    seata-server-1.3.0   配置: file.conf: registry.conf: application.yml配置: 配置中心配置文件: 数据库: 使用:

  7. CentOS系统 / 目录下每个子目录的作用

    Text. 1./bin 该目录存放root和交互式登录用户使用的二进制可执行文件,如cat,cp,date,rm等. 2./boot 该目录主要存放系统启动所需要的相关文件,如何内核文件vmlinu ...

  8. nginx增加自定义账号鉴权

    nginx增加自定义账号鉴权 使用nginx反向代理实现 当一个站点内部程序是个黑盒(无法修改里面的请求逻辑),如何增加自己的账号系统鉴权 实现逻辑 使用nginx的反向代理功能 自定义账号系统增加两 ...

  9. jenkins +docker+python接口自动化之jenkins容器下安装python项目所需要的库(三)

    1.场景 1.centos系统,在docker的jenkins容器下安装python项目所需要的包 2.我们目前是搞接口自动化,代码放到码云上,运行环境是jenkins容器下,所以需要在jenkins ...

  10. Taro 弹窗阻止小程序滑动穿透(亲测有效) tabbar数据缓存不更新 入口场景值不同

    v3.0 推出后反馈最多的问题之一,就是在 touchmove 事件回调中调用 e.stopPropagation() 并不能阻止滑动穿透. 这是因为 Taro 3 的事件冒泡机制是单独在小程序逻辑层 ...