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

相关推荐
炸膛坦客9 分钟前
C++ 学习与 CLion 使用:(四)常量和变量,包括字面常量和符号常量
开发语言·c++·学习
特立独行的猫a17 分钟前
C/C++三方库移植到HarmonyOS平台详细教程(补充版so库和头文件形式)
c语言·c++·harmonyos·napi·三方库·aki
zh_xuan1 小时前
LeeCode 40.组合总和II
c语言·数据结构·算法
wangluoqi2 小时前
c++ 数据结构-并查集、ST表 小总结
数据结构·c++
时间之里10 小时前
c++:MFC中sqlite3的使用(附实际案例)
c++·mfc·sqlite3
小白要加油努力10 小时前
C++设计模式--策略模式与观察者模式
开发语言·c++·设计模式
小马学嵌入式~11 小时前
数据结构:队列 二叉树
c语言·开发语言·数据结构·算法
John_ToDebug12 小时前
Chrome 内置扩展 vs WebUI:浏览器内核开发中的选择与实践
前端·c++·chrome
省四收割者12 小时前
Go语言入门(10)-数组
数据结构·经验分享·笔记·vscode·算法·golang
jiunian_cn13 小时前
【Linux】线程
android·linux·运维·c语言·c++·后端