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

相关推荐
dtq04243 小时前
C++小案例 - 日志实战
开发语言·c++
路灯上の马陈3 小时前
二叉树的定义与性质
数据结构·二叉树·树结构·1024程序员节
码匠许师傅4 小时前
【C++三方组件】Qt 概览:桌面 C++ 的事实标准与许可证三轨
开发语言·c++·qt
码匠许师傅4 小时前
【C++三方组件】POCO:跨平台系统编程全家桶
开发语言·c++
Bshaya_6 小时前
红黑树详解:概念、规则与插入实现
数据结构·红黑树·二叉搜索树·平衡树·插入逻辑
WBluuue7 小时前
数据结构与算法:分块(一):块状数组、块状链表
数据结构·c++·算法·链表
路灯上の马陈8 小时前
数据结构-树
数据结构·1024程序员节
必须得开心呀9 小时前
字节序大小端科普
c++·程序人生
benjormin9 小时前
Java数据结构 二叉搜索树
数据结构·算法
会编程的小孩9 小时前
C++ 子集
c++