要求

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

思路

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

实现

 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. Ceph 14.2.5-K8S 使用Ceph存储实战 -- <6>

    K8S 使用Ceph存储 PV.PVC概述 管理存储是管理计算的一个明显问题.PersistentVolume子系统为用户和管理员提供了一个API,用于抽象如何根据消费方式提供存储的详细信息.于是引入 ...

  2. C++并发与多线程学习笔记--async、future、packaged_task、promise

    async future packaged_task promise async std:async 是个函数,用来启动一个异步任务,启动起来一个异步任务之后,返回一个std::futre对象,启动一 ...

  3. [树状数组]数星星 Stars

    数 星 星 S t a r s 数星星 Stars 数星星Stars 题目描述 天空中有一些星星,这些星星都在不同的位置,每个星星有个坐标.如果一个星星的左下方(包含正左和正下)有 k k k 颗星星 ...

  4. 201871010203-陈鹏昱 实验二 个人项目—《D{0-1}KP问题》项目报告

    项目 内容 课程班级博客链接 班级博客 这个作业要求链接 作业要求 我的课程学习目标 运用科学高效的方法学习软件工程的理论和知识 这个作业在哪些方面帮助我实现学习目标 掌握软件项目个人开发流程,掌握G ...

  5. python3使用myqr生成链接二维码

    技术背景 二维码技术在各个领域中都已经有非常成熟的应用,比如随处可见的二维码支付,比如疫情期间的绿码,再比如工业领域中,可以使用二维码作为定位的标签,大大提升了室内定位技术的精确度.二维码的格式内容大 ...

  6. linux下Mysql 8.0.19 编译安装

    1 前言 linux下安装MySQL的方式有很多种,包括以仓库的方式安装(yum,apt,zypper),以包的方式安装(rpm,deb),以docker方式安装,从压缩包解压安装,从源码编译安装,这 ...

  7. 如何查看spark版本

    使用spark-shell命令进入shell模式

  8. Day14_81_反射机制获取Class属性

    反射机制获取Class属性 获取属性 方法一: Class对象 . getFields();只能用来获取公开的属性,不能获取有私有的或者受保护的属性 获取属性 方法二: Class对象 . getDe ...

  9. 02- HTML网页基础知识与浏览器介绍

    1.认识网页 网页主要由文字,图像和超链接等元素构成.当然,除了这些元素,网页还可以包含音频,视频,以及flask等. 如图所示就是一个网页: 网页是如何形成的呢? 它是由前端人员写的代码,经过浏览器 ...

  10. flex 的 三个参数 flex:1 0 auto

    flex :flex-group  flex-shirk  flex-basis ①.flex-group 剩余空间索取 默认值为0,不索取 eg:父元素400,子元素A为100px,B为200px. ...