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

相关推荐
XLYcmy26 分钟前
京东 算法实习一面 下+手撕
c++·python·llm·概率论·数据处理·训练·codebert
反转180度1 小时前
Qt 6 + C++17 实现 SFTP 批量部署:工业设备运维工具实战解析
运维·c++·qt
码匠许师傅2 小时前
【C++ 面试真题】聊聊 C++ 的多继承与虚继承
开发语言·c++·面试
有点。4 小时前
C++认识数
开发语言·c++
Herbert_hwt4 小时前
C语言零基础入门:循环控制与数据类型详解
c语言·数据结构·算法
2023自学中5 小时前
Qt 简易聊天室(第二篇),单线程 改为 多线程,方便后续增加文件传输功能
c++·qt
晚风醉蝶5 小时前
1-6-插入排序-InsertionSort
java·数据结构·排序算法
Tyler_TXZ6 小时前
C++C语言之——二叉树
c语言·开发语言·数据结构·c++·二叉树
有点。6 小时前
C++二叉树二(练习题)
数据结构·c++·算法·图论
不会代码的小猴6 小时前
C++新增关键字
开发语言·c++·笔记