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

相关推荐
繁华似锦respect6 分钟前
C++ & Linux 中 GDB 调试与内存泄漏检测详解
linux·c语言·开发语言·c++·windows·算法
立志成为大牛的小牛7 分钟前
数据结构——五十四、处理冲突的方法——开放定址法(王道408)
数据结构·学习·程序人生·考研·算法
子一!!7 分钟前
数据结构===红黑树===
数据结构
锡兰_CC11 分钟前
无缝触达,卓越体验:开启openEuler世界的任意门
服务器·网络·数据库·c++·图像处理·qt·nginx
王燕龙(大卫)26 分钟前
滑动窗口问题记录
c++
代码游侠30 分钟前
复习——栈、队列、树、哈希表
linux·数据结构·学习·算法
不会代码的小猴31 分钟前
C++的第十二天笔记
开发语言·c++·笔记
橘子真甜~33 分钟前
C/C++ Linux网络编程10 - http协议
linux·服务器·网络·c++·网络协议·http
蜗牛love天空1 小时前
bfs广度优先搜索-二叉树遍历
c++
刘家炫1 小时前
C++ 中的模版元编程
c++·现代c++·模版元编程