LeetCode_Maximum Depth of Binary Tree
一.题目
Maximum Depth of Binary Tree
Total Accepted: 70693 Total Submissions: 156776My
Submissions
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.
二.解题技巧
Depth of Binary Tree一样,都是对二叉树进行递归。然后将子树的最大深度进行返回。终于得到这个树的最大深度。
三.实现代码
#include <iostream> /**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/ 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 0;
} int Result = 1;
int Left = maxDepth(root->left);
int Right = maxDepth(root->right); if (Left * Right)
{
Result += Left < Right? Right : Left;
}
else
{
Result += Right + Left;
} return Result;
}
};
四.体会


LeetCode_Maximum Depth of Binary Tree的更多相关文章
- [LeetCode] Minimum Depth of Binary Tree 二叉树的最小深度
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- [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 ...
- [LintCode] Maximum Depth of Binary Tree 二叉树的最大深度
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- [Leetcode][JAVA] Minimum Depth of Binary Tree && Balanced Binary Tree && Maximum Depth of Binary Tree
Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...
- LeetCode:Minimum Depth of Binary Tree,Maximum Depth of Binary Tree
LeetCode:Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth ...
- LEETCODE —— binary tree [Same Tree] && [Maximum Depth of Binary Tree]
Same Tree Given two binary trees, write a function to check if they are equal or not. Two binary tre ...
- 33. Minimum Depth of Binary Tree && Balanced Binary Tree && Maximum Depth of Binary Tree
Minimum Depth of Binary Tree OJ: https://oj.leetcode.com/problems/minimum-depth-of-binary-tree/ Give ...
- Leetcode 111 Minimum Depth of Binary Tree 二叉树
找出最短的从叶子到根的路径长 可以回忆Maximum Depth of Binary Tree的写法,只不过在!root,我把它改成了10000000,还有max函数改成了min函数,最后的值如果是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 ...
随机推荐
- Mac OSX Yosemite 10.10 brew 错误:mktemp: mkdtemp failed on /tmp/git-LIPo: No such file or directory
这个问题困扰了我非常久非常久.使得我不得不花一点时间来说一下解决方法. 事情是这种:前两天兴高採烈的更新了一下宝贝mac到10.10. 一切看起来都那么美好,可是. .当我又一次安装magento的时 ...
- 初识cocos2dx——入门第一篇
享受"啪"的按下Enter的快感. (by云风) cocos2dx是不是个toy引擎,不同的人有不同的看法.可是你不得不承认它非常受欢迎.好多非常火的手游使用的就是cocos2dx ...
- 客户端通过wcf来启动或者停止服务器上的windows service
1.设置服务器上的windows service的security,下面的命令只能用cmd.exe来运行(以管理员模式) sc sdset "LISA_43_Dev_Batch" ...
- TLS握手
http://www.ruanyifeng.com/blog/2014/02/ssl_tls.html 1994年,NetScape公司设计了SSL协议(Secure Sockets Layer)的1 ...
- JQuery事件绑定,bind与on区别
jquery事件绑定bind:向匹配元素添加一个或多个事件处理器 $(selector).bind("click",data,function); live:向当前或未来的匹配元素 ...
- js闭包概念
含义:闭包是一个概念,它描述了函数执行完毕内存释放后,依然内存驻留的一个现象,只要把握这个核心概念,闭包就不难理解了 function a(){ var i=0; function ...
- Haskell手撸Softmax回归实现MNIST手写识别
Haskell手撸Softmax回归实现MNIST手写识别 前言 初学Haskell,看的书是Learn You a Haskell for Great Good, 才刚看到Making Our Ow ...
- Paper阅读总结Day1
Paper阅读总结Day1 1.Convolutional Neural Networks For Facial Expression Recognition 文章思想 简单的一篇关于表情识别的文章, ...
- Python IDE和编辑器
1.什么是IDE? IDE也就是集成开发环境,较常用的有PyCharm 2.编辑器 (1)Sublime Text Sublime Text 具有漂亮的用户界面和强大的功能,例如代码缩略图,Pytho ...
- Nusoap复杂对象的的webService制作
推荐网址:http://www.scottnichol.com/nusoapprogwsdl.htm摘抄部分如下:服务器端程序 <?php // Pull in the NuSOAP code ...