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

相关推荐
Hical_W9 分钟前
深入学习CPP17_PMR
c++·学习
不才小强30 分钟前
线性表详解:顺序与链式存储
数据结构·算法
计算机安禾1 小时前
【数据结构与算法】第42篇:并查集(Disjoint Set Union)
c语言·数据结构·c++·算法·链表·排序算法·深度优先
YuanDaima20481 小时前
二分查找基础原理与题目说明
开发语言·数据结构·人工智能·笔记·python·算法
苕皮蓝牙土豆2 小时前
Qt图形视图框架入门:坐标系统与鼠标事件处理详解
c++·qt
众少成多积小致巨2 小时前
libbinder_ndk 入门指南
前端·c++·架构
锅挤3 小时前
数据结构复习(第四章):串
数据结构
历程里程碑3 小时前
二叉树---翻转二叉树
开发语言·c++·elasticsearch·链表·搜索引擎·tornado·dash
闻缺陷则喜何志丹3 小时前
【排序】P6149 [USACO20FEB] Triangles S|普及+
c++·算法·排序·洛谷
tankeven3 小时前
HJ178 【模板】双指针
c++·算法