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

相关推荐
choumin6 小时前
C++中万能引用与std::forward的作用和用法
c++·万能引用·std forward
下一站春风里7 小时前
位运算完整学习笔记:按位与(&)+ 异或(^)
开发语言·c++·算法
挖井观天8 小时前
《Java数据结构》第一讲:顺序表
数据结构
努力努力再努力wz9 小时前
【C++高阶系列】C++20 协程底层原理:函数为什么能暂停?从状态机到 Task,彻底串起挂起、恢复与协程调度
开发语言·c++·c++20·1024程序员节
划破黑暗的第一缕曙光10 小时前
[C++]:4.模板
c++·template·模板·函数模版·类模版
十五年专注C++开发10 小时前
pybind11:一个跨C++和Python平台的编程库
开发语言·c++·python·pybind11
CodeYp201910 小时前
第11讲:stack 和 queue
开发语言·c++·queue·stack
炘爚10 小时前
数据结构算法总结
数据结构·python·算法
沫璃染墨10 小时前
《Qt从零入门系列(十五):Qt线程安全与锁机制详解——从QMutex到RAII与读写锁实战》
开发语言·c++·qt·安全·交互·线程·qt6.3
hetao173383710 小时前
2026-10-25 hetao1733837 的刷题记录
c++·算法