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

相关推荐
今天要早睡_34 分钟前
C++15_deque与容器适配器复习
java·c++·rpc
@三十一Y1 小时前
C++高阶数据结构:图
开发语言·数据结构·c++
骑着蜗牛撵大象3272 小时前
从0实现C++ AI大模型接入SDK(十):DataManager业务实现——数据库CRUD、事务管理与数据一致性
数据库·c++·人工智能·sqlite·事务管理·数据一致性
夏日听雨眠2 小时前
数据结构( Floyd)
数据结构·算法
金榜题名萱2 小时前
蓝桥杯 - 妮妮的月饼工厂(二分)
c语言·数据结构·c++·算法·蓝桥杯·动态规划
小王C语言3 小时前
优选算法:队列+宽选(bfs)
数据结构·算法·宽度优先
充电zcx3 小时前
C++ 复习:继承——访问规则、隐藏、多继承与虚继承
c++·继承·面向对象·多继承·虚继承
用户3900341061483 小时前
C++ 移动语义到底移走了什么?从 std::string 的 32 字节布局讲起
c++
HQL_seven3 小时前
【数据结构】线性结构
数据结构
ShineWinsu3 小时前
对于Qt:QSS样式属性的解析
开发语言·c++·qt·面试·盒子模型·客户端·qss