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

相关推荐
郝学胜-神的一滴5 小时前
C++ Templates 01:拨开迷雾,读懂C++模板经典著作
开发语言·c++·程序人生·游戏·游戏引擎·软件构建
坤坤子吖6 小时前
算法学习——高精度加减乘除
c++·笔记·学习·算法
朝朝辞暮i6 小时前
C++ 第 14 课:引用 &
开发语言·c++·算法
白山编程大哥6 小时前
适合初学者练习的 C/C++ 开源项目推荐
c语言·c++·开源
C++ 老炮儿的技术栈6 小时前
有符号变量与无符号变量的区别
服务器·开发语言·前端·数据结构·c++·算法·c
个 人 练 习 生6 小时前
C++:模板初阶详解
开发语言·c++·经验分享·学习
知识分享小能手7 小时前
C++ 学习教程,从入门到精通,C++类继承 — 完整知识点详解(13)
开发语言·c++·学习
YYRAN_ZZU7 小时前
如何编一个gcc的交叉工具链
linux·c++
Frank_refuel7 小时前
【C++】手撕JRpc框架(梳理RPC流程)
c++
wuminyu7 小时前
JVM虚拟线程的底层实现原理分析
java·linux·c语言·jvm·c++