185 字
1 分钟
LeetCode-104.二叉树的最大深度

二叉树#

104. 二叉树的最大深度

给定一个二叉树 root ,返回其最大深度。

二叉树的 最大深度 是指从根节点到最远叶子节点的最长路径上的节点数。


示例 1:

img

输入:root = [3,9,20,null,null,15,7]
输出:3

示例 2:

输入:root = [1,null,2]
输出:2

递归的解法

递归这种题有时候就是很傻逼和神奇,就像这题和汉谟拉比,不同的是这题从逻辑上可以从下往上推出来,汉谟拉比很麻烦

/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func maxDepth(root *TreeNode) int {
if root == nil {
return 0
}
leftHeight := maxDepth(root.Left)
rightHeight := maxDepth(root.Right)
return max(leftHeight, rightHeight) + 1
}
LeetCode-104.二叉树的最大深度
https://sheep44044.github.io/posts/算法/二叉树/leetcode-104二叉树的最大深度/
作者
sheep44044
发布于
2026-03-14
许可协议
CC BY-NC-SA 4.0