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

相关推荐
玖釉-4 分钟前
Windows 下 VS2022 编译运行 Khronos Vulkan Samples 全避坑指南
c++·windows·图形渲染
星火开发设计15 分钟前
C++ 分支结构:if-else 与 switch-case 的用法与区别
开发语言·c++·学习·算法·switch·知识·分支
txzrxz17 分钟前
数据结构有关的题目(栈,队列,set和map)
数据结构·c++·笔记·算法··队列
Two_brushes.23 分钟前
C++ 常见特殊类的设计(含有单例模式)
开发语言·c++
CoderCodingNo28 分钟前
【GESP】C++五级练习题(前缀和) luogu-P1114 “非常男女”计划
数据结构·c++·算法
我是大咖31 分钟前
关于柔性数组的理解
数据结构·算法·柔性数组
阿班d39 分钟前
33333333
c++
charlee4441 分钟前
C++ 封装 C FFI 接口最佳实践:以 Hugging Face Tokenizer 为例
c++·智能指针·tokenizer·ffi·raii
Once_day1 小时前
CC++八股文之内存泄漏
c语言·c++
wen__xvn1 小时前
代码随想录算法训练营DAY18第六章 二叉树part06
数据结构