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. PHP dirname() 返回路径中的目录部分basename() 函数返回路径中的文件名部分。

    dirname (PHP 4, PHP 5) dirname — 返回路径中的目录部分说明string dirname ( string $path ) 给出一个包含有指向一个文件的全路径的字符串,本 ...

  2. Linux中__init、__devinit等内核优化宏【转】

    转自:http://blog.csdn.net/joker0910/article/details/7171626 内核使用了大量不同的宏来标记具有不同作用的函数和数据结构.如宏__init .__d ...

  3. 如何给桌面搬家(Win XP)

    是不是习惯把一些常用的文件放在桌面上?或者接收个文件就直接放桌面了,这样用起来方便点. 可是一旦你重装系统或者恢复系统,桌面又回到了以前的状态,很多的文件就丢了.小心的话,重做系统前会做个备份.但如果 ...

  4. 【转】对Android开发者有益的40条优化建议

    下面是开始Android编程的好方法: 找一些与你想做事情类似的代码 调整它,尝试让它做你像做的事情 经历问题 使用StackOverflow解决问题 对每个你像添加的特征重复上述过程.这种方法能够激 ...

  5. maven3实战之设置HTTP代理

    maven3实战之设置HTTP代理 ---------- 有时候你所在的公司基于安全因素考虑,要求你使用通过安全认证的代理访问因特网.这种情况下,就需要为Maven配置HTTP代理,才能让它正常访问外 ...

  6. Nginx proxy_pass 加与不加 "/" 区别

    下面四种情况分别用http://192.168.1.100/proxy/test.html 进行访问. 第一种: location  /proxy/ { proxy_pass http://127.0 ...

  7. codeVS1966 乘法游戏

    区间dp. 用f[l][r]代表从l合并到r的最小得分. 显然 r-l<=1时,f[l][r]=0. 对区间dp一直很不熟悉,得多练练了. #include<cstdio> #inc ...

  8. 51nod1119 机器人走方格 V2

    终于学到了求组合数的正确姿势 //C(n+m-2,m-1) #include<cstdio> #include<cstring> #include<cctype> ...

  9. VC++菜单

    2.1 如何动态添加和删除菜单项 AppendMenu InsertMenu DeleteMenu ////////////////////////////////////////////////// ...

  10. C# 创建系统服务并定时执行【转载】

    [转载]http://www.cnblogs.com/hfzsjz/archive/2011/01/07/1929898.html C# 创建系统服务并定时执行 1.新建项目 --> Windo ...