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;
    }
    };

相关推荐
xys_6786 小时前
深挖数据结构:树和二叉树(上)
数据结构·算法
BeiGuTing_code6 小时前
Java动态数组(一)
java·数据结构·windows
衍玊6 小时前
多路转接原理与应用
c++
himobrinehacken8 小时前
位运算揭秘:左右移与符号位陷阱
c语言·c++·算法·安全
Code_Solitude10 小时前
传智杯备赛代码整理(前4题)
数据结构·算法
Jasmine_llq11 小时前
《P8816 [CSP-J 2022] 上升点列》
数据结构·算法·贪心·排序·二维动态规划 dp·类背包状态转移
Android小码家11 小时前
Vehicle 实战 自定义属性+测试demo
java·c++·framework·vhal·vehicle
hetao173383711 小时前
2026-10-19 hetao1733837 的刷题记录
c++·算法
夏虫不可与之言冰11 小时前
第三课 点亮 LED 与定时器非阻塞闪烁
c语言·c++·vscode·单片机·嵌入式硬件·物联网
辛苦才能11 小时前
C++ 哈希表深度解析:删除为什么必须打标记,负载因子为什么卡 0.7
开发语言·数据结构·c++·算法