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

相关推荐
小保CPP1 小时前
OCR C++ Tesseract基础用法
c++·人工智能·ocr·模式识别·光学字符识别
TCW11211 小时前
AI底层系列:用C++实现线性代数的公式推导与算法设计-8.线性变化(3)
c++·人工智能·算法
皓月斯语2 小时前
B3849 [GESP样题 三级] 进制转换 题解
c++·算法·题解
Ljwuhe2 小时前
C++——模板进阶
开发语言·c++
hehelm2 小时前
AI 大模型接入 SDK —项目概述
linux·服务器·网络·数据库·c++
牢姐与蒯2 小时前
双指针算法
数据结构·算法
Hesionberger2 小时前
LeetCode406:重建身高队列精髓解析
开发语言·数据结构·python·算法·leetcode
sTone873753 小时前
写时复制COW的第一性理解
android·c++·flutter
CHANG_THE_WORLD4 小时前
逐层拆解:C++ 虚函数从对象内存到手工调用的完整过程
java·开发语言·c++
Scott9999HH4 小时前
2026 避坑实录:国产品牌压力变送器什么牌子好?从硬件抗扰到 C++ Qt 实时曲线绘制源码剖析
开发语言·c++·qt