104. 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

Constraints:

  • The number of nodes in the tree is in the range [0, 104].

  • -100 <= Node.val <= 100

    class Solution {
    public:
    int maxDepth(TreeNode* root) {
    if(root==nullptr)return 0;
    return max(maxDepth(root->left),maxDepth(root->right))+1;
    }
    };

相关推荐
王老师青少年编程1 天前
信奥赛C++提高组csp-s之欧拉回路
c++·算法·csp·欧拉回路·信奥赛·csp-s·提高组
No0d1es1 天前
2025年12月 GESP CCF编程能力等级认证C++六级真题
c++·青少年编程·gesp·ccf·6级
Terrence Shen1 天前
【CUDA编程系列】之01
c++·人工智能·深度学习·机器学习
墨有6661 天前
数学分析栈的出栈顺序:从算法判断到数学本质(卡特兰数初探)
c++·算法·数学建模
liulilittle1 天前
LIBTCPIP 技术探秘(tun2sys-socket)
开发语言·网络·c++·信息与通信·通信·tun
yyy(十一月限定版)1 天前
c++(3)类和对象(中)
java·开发语言·c++
s砚山s1 天前
代码随想录刷题——二叉树篇(十三)
数据结构·算法
DYS_房东的猫1 天前
写出第一个程序
c++
ulias2121 天前
AVL树的实现
开发语言·数据结构·c++·windows
黎雁·泠崖1 天前
二叉树知识体系全梳理:从基础到进阶一站式通关
c语言·数据结构·leetcode