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.

Solution:

 /**
* Definition for a binary tree node.
* 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)
return ;
else if(root->left && !root->right)
return +maxDepth(root->left);
else if(!root->left && root->right)
return +maxDepth(root->right);
else
return +max(maxDepth(root->right),maxDepth(root->left));
}
};

【LeetCode】104 - Maximum Depth of Binary Tree的更多相关文章

  1. 【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 ...

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

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

  3. 【一天一道LeetCode】#104. Maximum Depth of Binary Tree

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源:http ...

  4. 【easy】104. Maximum Depth of Binary Tree 求二叉树的最大深度

    求二叉树的最大深度 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; ...

  5. 【LeetCode】662. Maximum Width of Binary Tree 解题报告(Python)

    [LeetCode]662. Maximum Width of Binary Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.co ...

  6. 【LeetCode】111. Minimum Depth of Binary Tree (2 solutions)

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

  7. LeetCode OJ 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(easy)

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

  9. 【LeetCode】111. Minimum Depth of Binary Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 [LeetCode] 题目地址 ...

随机推荐

  1. 关于Linux操作系统下文件特殊权限的解释

    文件特殊权限的解释. -rwsr-xr-x = 4755 文件执行的时候,会以owner的身份来执行,就是setuid . 例如:-rwxr-xr-t 1 root wheel 0 7 9 18:24 ...

  2. jsp %EF%BB%BF

    utf-8 bom 问题,页面中存在不可忽略的空白,去掉即可 很微妙的一个空格,不易发现

  3. url重写(伪静态)IIS配置图解

    通过IIS创建虚拟目录,新建网站放在该目录下面: IIS配置: 虚拟目录=>点击右键=>选择属性: 点击配置: 点击编辑: 复制两个文本框文本到记事本中=>点击确定: 点击添加=&g ...

  4. 文件夹工具类 - FolderUtils

    文件夹工具类,提供创建完整路径的方法. 源码如下:(点击下载 -FolderUtils.java .commons-io-2.4.jar ) import java.io.File; import o ...

  5. JavaScript —— 如何判断一个非数字输入

    在页面里,如何用JS去判断一个用户输入是不是一个数字. 你是不是首先想到了正则表达式? JS里有个现成的函数,isNaN(x) isNaN(x) 函数可用于判断其参数是否是 NaN(Not a Num ...

  6. java 反编译插件 JD-Eclipse 和 JD-IntelliJ

    去官网 : http://jd.benow.ca/ 找到 JD-Eclipse 和  JD-IntelliJ,下载,前者安在eclipse上,后者安在as上.

  7. NDK(5) Android JNI官方综合教程[JavaVM and JNIEnv,Threads ,jclass, jmethodID, and jfieldID,UTF-8 and UTF-16 Strings,Exceptions,Native Libraries等等]

    JNI Tips In this document JavaVM and JNIEnv Threads jclass, jmethodID, and jfieldID Local and Global ...

  8. 《OD学hadoop》第三周0709

    一.MapReduce编程模型1. 中心思想: 分而治之2. map(映射)3. 分布式计算模型,处理海量数据4. 一个简单的MR程序需要制定map().reduce().input.output5. ...

  9. Qt Assistant介绍

    简介 Qt Assistant也就是我们常说的Qt助手,是一款用于呈现在线文档的工具. 简介 一分钟学会使用 Qt参考文档 Qt Assistant详解 命令行选项 工具窗口 文档窗口 工具栏 菜单 ...

  10. jQuery1.9+中删除了live以后的替代方法

    .live() removed The .live() method has been deprecated since jQuery 1.7 and has been removed in 1.9. ...