题目

求二叉树的深度,即根节点出发的最长路径上点的个数,即最长路径+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. 快速搭建maven私服仓库并配置

    1.第一步,通过官方网站下载tar.gz包上传到服务器并解压 随后进入解压目录,并执行./bin/nexus start 此时可能会出现如下异常 处理这个问题很容易,找到bin下的nexus文件,vi ...

  2. SpringBoot配置阿里云https提示端口占用问题

    1.因为要配置https,所以去网上找了一些资料,然后按照步骤,依次完成了以下步骤 ①在application.yml中加入配置 http: port: 12000 #原本的端口号server: po ...

  3. React中使用CSS的N种方式

    1.在组件中直接使用style,注意,div1各个属性值加双引号 const div1 = { width: "300px", margin: "30px auto&qu ...

  4. 树莓派4B安装Gogs

    https://www.labno3.com/2021/01/28/how-to-install-gogs-on-the-raspberry-pi/ https://gogs.io/docs/inst ...

  5. iview table配合Tooltip使用的时候 气泡错位的问题

    Tooltip 气泡错位 点击table的单元格,实现遮盖文字的预览功能,但是气泡会发生偏移,看了Tooltip的样式发现 气泡是绝对定位, 想到子绝父相,于是给表格的td添加相对定位的功能,结果能解 ...

  6. express的使用:接口的编写(三)

    1.接口的跨域问题 a.CORS,主流 b.JSONP,只支持get请求 步骤:a.安装 npm install cors b.使用  const cors = require('cors') 导入中 ...

  7. aspose word导出表格

    [HttpGet] [Route("GetPurchaseItemWord")] public IHttpActionResult Get_PurchaseItemWord(str ...

  8. kibana启动时报错:Validation Failed: 1: this action would add [2] total shards, but this cluster currently has [999]/[1000] maximum shards open

    解决方案: curl -XPUT -H "Content-Type:application/json" -d '{"persistent":{"clu ...

  9. PWM脉宽调制

    PWM(pulse width modulation) .由微处理器输出一系列占空比不同的矩形脉冲(单个周期相同),应用在测量,通信,功率控制与变换的许多领域.优点是从微处理器到被控系统的信号都是数字 ...

  10. mongodb地理位置坐标加了索引,操作时报错 Location object expected, location array not in correct format

    别犹豫了,将坐标中的数据改为数字类型即可,如: location:[113.45,34,191]