题目

求二叉树的深度,即根节点出发的最长路径上点的个数,即最长路径+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. linux下第三方库的配置和链接——以opencv为例

    安装OpenCV(可参考链接) 下载source到 /usr/local/路径下 新建 /build/ cmake 编译 添加库路径 vim /etc/ld.so.conf 该目录作用参考链接 输入: ...

  2. JSON::ParserError - 416: unexpected token at

    rm -rf ~/.cocoapods/repos/Spec_Lockandrm -rf ~/.cocoapods/repos/trunk/

  3. Linux内核启动-从入口到start_kernel

    目录 1. 内核启动要求 2. 内核启动入口 3. 概览:从入口到start_kernel 4. MMU开启之前:primary_entry 4.1. preserve_boot_args 4.2. ...

  4. Posggresql插件Multicorn安装问题总结

    根据官网https://multicorn.readthedocs.io/en/latest/installation.html的安装指南下载安装,关键信息: Postgresql 9.1+ Post ...

  5. ionic 架构

    1.路由:rout,页面内容:html,页面css:scss,js脚本内容:ts 2.功能介绍 路由:负责组织每个页面. 页面css:scss,负责定制每个组件的内容,比如组件是iobag,那么在这个 ...

  6. 获取网页数据 Qt 从客户端发起http响应

    Qt 从客户端发起http响应 获取网页数据实现代码 void Test::GetHttp(QString strServerIP,QString strUserName,QString strPas ...

  7. Easycode—MybatisPlus模板

    EasyCode使用指南 1.下载EasyCode插件 2.配置EasyCode 2.1.配置作者名称              2.2.配置代码内容生成模板(模板内容见文末)            ...

  8. linux挂载文件服务器

    smbclient -L //192.168.1.1/ -U administrator //直接挂载 使用下面这条命令就行 sudo mount -t cifs -o username=文件服务器账 ...

  9. Pytest Fixture(三)

    name: name参数表示可以对fixture的名称进行重命名: 注意:通过name重命名后,继续使用以前的名字调用会报错. import pytest @pytest.fixture(name=' ...

  10. python菜鸟学习: 3.浅copy使用场景

    # -*- coding: utf-8 -*-import copy# 浅copy# 使用场景,比如A,B夫妻共有一个银行账户,存取马宁的数据username = ["name", ...