[LeetCode 题解]: Maximum Depth of Binary Tree
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.
题解: 题意比较清楚, 找到从root出发最长的一条路径的长度。 采用DFS即可。
相似的一道题: Minimux Depth of Binary Tree 解法: http://www.cnblogs.com/double-win/p/3737237.html
/**
* 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 minDepth(TreeNode *root) {
if(root==NULL) return ;
if(root->left==NULL && root->right==NULL) return ; int Leftdepth = minDepth(root->left);
int Rightdepth = minDepth(root->right); if(Leftdepth==)
return Rightdepth+;
else if(Rightdepth==)
return Leftdepth+; return min(Leftdepth,Rightdepth)+;
}
};
[LeetCode 题解]: Maximum Depth of Binary Tree的更多相关文章
- 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 ...
- leetcode 104 Maximum Depth of Binary Tree二叉树求深度
Maximum Depth of Binary Tree Total Accepted: 63668 Total Submissions: 141121 My Submissions Question ...
- LeetCode 104. Maximum Depth of Binary Tree C++ 解题报告
104. Maximum Depth of Binary Tree -- Easy 方法 使用递归 /** * Definition for a binary tree node. * struct ...
- [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 ...
- (二叉树 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- 获取lable选中时触发事件
通常做网页时不会用radio和checkbox的原有样式, 会进行样式美化. 如何在点击checkbox时触发一个事件呢? <div class="main-checkbox" ...
- django -- url (模版语言{{ request.path_info }})
在django的模版语言中中可以使用 {{ request.path_info }} 帮助生成url. urls.py from django.conf.urls import url, incl ...
- c++ 字符检测 TCharacter
c++ 字符检测 IsSurrogatePair,IsHighSurrogate,IsLowSurrogate,ConvertToUtf32http://docwiki.embarcadero.com ...
- Django 实现用户认证set_Cookie
当用户通过认证时,set_Cookie(key, value) request.Cookie.get(key) 如果key不为空,就说明验证通过,否者重新跳转回login登录页面 对于URL urlp ...
- java 整数存储为2进制补码形式
今天早上看java的源代码,发现: 用计算器转成十进制后是下面这个值: 然后我就纳闷了,Integer的最小值,不可能怎么大吧? 于是果断写代码验证: 谜底揭开: 0x80000000 是Intege ...
- 使用myeclipse开发java,解决java中继承JFrame类出现The type JFrame is not accessible due to restriction的问题
在java中创建窗体,导入了java中的JFrame类,之后会出现错误: Access restriction: The type QName is not accessible due to res ...
- mysqldump test
CREATE TABLE IF NOT EXISTS `runoob_tbl`( `runoob_id` INT UNSIGNED AUTO_INCREMENT, `runoob_title` VAR ...
- Spark之 Spark Streaming流式处理
SparkStreaming Spark Streaming类似于Apache Storm,用于流式数据的处理.Spark Streaming有高吞吐量和容错能力强等特点.Spark Streamin ...
- 简单HttpClientUtils工具类
package com.zy.utils; import org.apache.http.HttpEntity; import org.apache.http.HttpStatus; import o ...
- 【HDU - 5442】Favorite Donut 【最大表示法+KMP/后缀数组】
题意 给出一个长度为n的环状由小写字母组成的序列,请找出从何处断开,顺时针还是逆时针,使得字典序最大.如果两个字符串的字典序一样大,那么它会选择下下标最小的那个.如果某个点顺时针逆时针产生的字典序大小 ...