题目

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

  1. Input: root = [3,9,20,null,null,15,7]
  2. Output: 3

Example 2:

  1. Input: root = [1,null,2]
  2. Output: 2

Example 3:

  1. Input: root = []
  2. Output: 0

Example 4:

  1. Input: root = [0]
  2. Output: 1

思路

DFS思路参考最长直径https://leetcode.com/problems/diameter-of-binary-tree/

这里只用找到一边,不用相加,所以是max(left,right)

代码

  1. class Solution {
  2. public int maxDepth(TreeNode root) {
  3. int ans=dfs(root,0);
  4. return ans;
  5. }
  6.  
  7. public int dfs(TreeNode root,int ans){
  8. if(root==null)
  9. return 0;
  10. int left=dfs(root.left,ans);
  11. int right=dfs(root.right,ans);
  12. ans+=Math.max(left,right);
  13. return ans+1;
  14. }
  15. }

[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. Centos7 安装Seata,注册中心选择nocas

    前言 1.什么是分布式事务,分布式事务包含哪些角色 分布式事务主要包含,事务参与者,事务管理者,资源服务器三个角色.分布式事务是指着三个角色分别位于不同的分布式系统的不同节点之上.简单的说,就是一次大 ...

  2. 论文笔记:Access Path Selection In A Relational Database Management System

    论文笔记:Access Path Selection In A Relation Database Management System 这篇文章是 1979 年由 IBM 发表的.主要介绍了 Syst ...

  3. JavaScript逗号运算符的用法

    var a = 3, b b = (a++, a) 与 var a = 3, b b = a++ 区别

  4. 【笔记】DDD实战课-人保架构欧创新

    目录 开篇 学好DDD,你能做什么? 基础 领域驱动设计:微服务设计为什么要选择 DDD? DDD的两层设计 DDD与微服务的关系 领域.子域.核心域.通用域和支撑域:傻傻分不清? 领域和子域 核心域 ...

  5. .net ef 链接 mysql

    https://blog.csdn.net/weixin_30394975/article/details/114168133

  6. java的知识点

    java 知识点 1.包装类自带有parse方法 Integer i = 315; int i1 = Integer.parseInt("315"); System.out.pri ...

  7. 03 Proxmox VE介绍

    突破困境! 企业开源虚拟化管理平台 使用Proxmox Virtual Environment 郑郁霖(Jason Cheng)著 版次:2021年12月初版 03 Proxmox VE介绍 3.1 ...

  8. HTML弹出对话框功能大全

    注://关闭,父窗口弹出对话框,子窗口直接关闭 this.Response.Write("<script language=javascript>window.close();& ...

  9. 前端电商 sku 的全排列算法

    需求 需求描述起来很简单,有这样三个数组: let names = ["iPhone",'iPhone xs'] let colors = ['黑色','白色'] let stor ...

  10. EIRENE GSM-R编码计划

    Numbering plan overview This appendix provides an overview of the numbering plan as defined in this ...