要求

  • 求一棵二叉树的最高深度

思路

  • 递归地求左右子树的最高深度

实现

 1 Definition for a binary tree node.
2 struct TreeNode {
3 int val;
4 TreeNode *left;
5 TreeNode *right;
6 TreeNode(int x) : val(x), left(NULL), right(NULL) {}
7 };
8
9 class Solution {
10 public:
11 int maxDepth(TreeNode* root) {
12
13 if( root == NULL )
14 return 0;
15
16 return max( maxDepth( root->left ),maxDepth( root->right )) + 1;
17 }
18 };

相关

  • 111 Minimum Depth of Binary Tree

[刷题] 104 Maximum Depth of Binary Tree的更多相关文章

  1. LeetCode Javascript实现 258. Add Digits 104. Maximum Depth of Binary Tree 226. Invert Binary Tree

    258. Add Digits Digit root 数根问题 /** * @param {number} num * @return {number} */ var addDigits = func ...

  2. 104. Maximum Depth of Binary Tree(C++)

    104. Maximum Depth of Binary Tree Given a binary tree, find its maximum depth. The maximum depth is ...

  3. LeetCode 104. Maximum Depth of Binary Tree C++ 解题报告

    104. Maximum Depth of Binary Tree -- Easy 方法 使用递归 /** * Definition for a binary tree node. * struct ...

  4. leetcode 104 Maximum Depth of Binary Tree二叉树求深度

    Maximum Depth of Binary Tree Total Accepted: 63668 Total Submissions: 141121 My Submissions Question ...

  5. 【LeetCode】104. Maximum Depth of Binary Tree (2 solutions)

    Maximum Depth of Binary Tree  Given a binary tree, find its maximum depth. The maximum depth is the ...

  6. [LeetCode] 104. Maximum Depth of Binary Tree 二叉树的最大深度

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

  7. (二叉树 BFS DFS) leetcode 104. Maximum Depth of Binary Tree

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

  8. 【LeetCode】104. Maximum Depth of Binary Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:BFS 方法二:DFS 参考资料 日期 题目 ...

  9. leetCode 104.Maximum Depth of Binary Tree(二叉树最大深度) 解题思路和方法

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

随机推荐

  1. CPU 权限划分

    Intel的CPU将特权级别分为4个级别:RING0,RING1,RING2,RING3.Windows只使用其中的两个级别RING0和RING3,RING0只给操作系统用,RING3谁都能用.如果普 ...

  2. CVPR2021| 行人搜索中的第一个anchor-free模型:AlignPS

    论文地址:https://arxiv.org/abs/2103.11617 代码地址:https://github.com/daodaofr/AlignPS 前言: 本文针对anchor-free模型 ...

  3. Elasticsearch 主节点和暖热节点解析

    Elasticsearch 主节点和暖热节点解析 主节点 控制整个集群,进行一些轻量级操作,列如:跟踪哪些节点是集群中的一部分,决定节点分片分配,负责集群健康, 不包含数据,也不参与搜索和索引操作,对 ...

  4. C#异步编程由浅入深(二)Async/Await的作用.

      考虑到直接讲实现一个类Task库思维有点跳跃,所以本节主要讲解Async/Await的本质作用(解决了什么问题),以及Async/Await的工作原理.实现一个类Task的库则放在后面讲.首先回顾 ...

  5. 02_利用numpy解决线性回归问题

    02_利用numpy解决线性回归问题 目录 一.引言 二.线性回归简单介绍 2.1 线性回归三要素 2.2 损失函数 2.3 梯度下降 三.解决线性回归问题的五个步骤 四.利用Numpy实战解决线性回 ...

  6. 动态的创建Class对象方法及调用方式性能分析

    有了Class对象,能做什么? 创建类的对象:调用Class对象的newInstance()方法 类必须有一个无参数的构造器. 类的构造器的访问权限需要足够. 思考?没有无参的构造器就不能创建对象吗? ...

  7. 6.2set用法

    目录 1.set的定义 2.set容器内元素的访问 3.set常见使用的函数 set可以内部进行自动递增排序,且自动去除了重复元素 1.set的定义 set<typename> name; ...

  8. 创建逻辑卷,格式化为xfs格式化,在线扩容

    创建逻辑卷,并且格式化为xfs格式化好,然后在线扩容 删除逻辑卷组

  9. (九)VMware Harbor 项目管理-上传/下载镜像

    VMware Harbor项目管理 Harbor中的项目包含应用程序的所有存储库. Harbor有两类项目: 公共:所有用户都拥有公共项目的读取权限,您可以方便地以这种方式与其他人共享一些存储库. 私 ...

  10. SpringBoot 启动慢?那是因为你不知道它

    前言 在 2021 年这个小学作文中的未来年份,没有想象中的汽车满天飞,也没有实现机器人满地跑.但牛逼的是我们都有一个共识: 知乎达到了人均 "谢邀~ 人在美国刚下飞机"的生活水平 ...