可能是因为我是按难度顺序刷的原因,这个其实在之前的几道题里面已经写过了。题目如下:

Given a binary tree, find its maximum depth.

The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

然后就直接拿之前的代码了:

/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int maxDepth(TreeNode *root) {
if (root == NULL)
return 0;
else
{
int aspros = maxDepth(root->left);
int defteros = maxDepth(root->right);
return 1 + (aspros>defteros ? aspros : defteros);
}
}
};

非常简单,重复复用。

[leetcode] 8. Maximum Depth of Binary Tree的更多相关文章

  1. Leetcode | Minimum/Maximum Depth of Binary Tree

    Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...

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

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

  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 二叉树的最大深度

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

  5. (二叉树 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 ...

  6. [LeetCode 题解]: 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. 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

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

  9. leetcode 104 Maximum Depth of Binary Tree ----- java

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

  10. Java [Leetcode 104]Maximum Depth of Binary Tree

    题目描述: Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along th ...

随机推荐

  1. boost::threadpool 调用类成员变量并传入参数 的方法

    1. 首先到官网下载   http://threadpool.sourceforge.net/   2. 包含头文件   #include "../boost/threadpool.hpp& ...

  2. 关于lazyload图片延迟加载简单介绍

    LazyLoad大家再熟悉不过的一个jquery插件了,它可以延迟加载长页面中的图片. 也就是说在浏览器可视区域外的图片不会被载入,直到用户将页面滚动到它们所在的位置才会加载并显示出来,这和图片预加载 ...

  3. 20181205_C#窗体监听键盘事件

    1. 需要设置窗体的   KeyPreview = true; 2. 如果窗体上有获取的了焦点的button按钮, 则监听不到 Enter事件, 需要取消按钮的焦点

  4. 第2章 Linux操作系统简介

    1. Linux操作系统的构成 (1)内核(kernel) ①操作系统的核心,负责管理系统的进程.内存.设备驱动程序.文件和网络系统. ②控制系统和硬件之间的相互通信. ③决定着系统的性能和稳定性. ...

  5. Docker系列05:docker镜像制作 &Docker file

    1 什么是Dockerfile? Dockerfile是一个包含用于组合映像的命令的文本文档.可以使用在命令行中调用任何命令. Docker通过读取Dockerfile中的指令自动生成映像. dock ...

  6. mysql修复表

    数据库Table xxx is marked as crashed and should be repaired错误的解决方法服务器断电等原因可能导致数据表损坏,导致访问的时候提示:Table xxx ...

  7. Shell脚本的调试方法

    Shell脚本的调试方法 Shell提供了一些用于调试脚本的选项,如下所示: -n    读一遍脚本中的命令但不执行,用于检查脚本中的语法错误 -v    一边执行脚本,一边将执行过的脚本命令打印到标 ...

  8. linux下mysql的安装配置

    http://blog.csdn.net/xiagege3/article/details/41852895   (实战用的此文,要求mysql源码必须是未编译的,需依赖cmake编译) http:/ ...

  9. Basic64 编码解码

    import sun.misc.BASE64Decoder; public class Base64 { /** * 字符串转Base64编码 * @param s * @return */ publ ...

  10. y=x^2 vs y=x^(1/2)

    [y=x^2 vs y=x^(1/2)] y=x^2,基础函数,废话不多说. y=x^(1/2),指数变成了上式的倒数.x^(1/2)即是,√x.但函数图像会是什么样呢?可以把y=x^(1/2),转变 ...