Maximum Depth of Binary Tree,求树的最大深度
算法分析:求树的最小最大深度时候,都有两种方法,第一种是递归思想。树最大最小深度,即为它的子树的最大最小深度+1,是动态规划的思想。还有一种方法是层序遍历树,只不过求最小深度时,找到第一个叶子节点就可以返回,该节点的深度,即为树的最小深度。求最大深度时,需要层序遍历完整棵树。
public class MaximumDepthofBinaryTree
{
public int maxDepth(TreeNode root)
{
if(root == null)
{
return 0;
}
int lmax = maxDepth(root.left);
int rmax = maxDepth(root.right);
if(lmax == 0 && rmax == 0)
{
return 1;
}
if(lmax == 0)
{
return rmax + 1;
}
if(rmax == 0)
{
return lmax + 1;
}
return Math.max(lmax, rmax) + 1;
} public int maxDepth2(TreeNode root)
{
if(root == null)
{
return 0;
}
ArrayList<TreeNode> list = new ArrayList<>();
list.add(root);
int count = 0;
while(!list.isEmpty())
{
ArrayList<TreeNode> temp = new ArrayList<>();
for (TreeNode node : list) {
if(node.left != null)
{
temp.add(node.left);
}
if(node.right != null)
{
temp.add(node.right);
}
}
count ++;
list = temp;
}
return count;
}
}
Maximum Depth of Binary Tree,求树的最大深度的更多相关文章
- LeetCode Maximum Depth of Binary Tree (求树的深度)
题意:给一棵二叉树,求其深度. 思路:递归比较简洁,先求左子树深度,再求右子树深度,比较其结果,返回:max_one+1. /** * Definition for a binary tree nod ...
- 【easy】104. Maximum Depth of Binary Tree 求二叉树的最大深度
求二叉树的最大深度 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; ...
- 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二叉树求深度
Maximum Depth of Binary Tree Total Accepted: 63668 Total Submissions: 141121 My Submissions Question ...
- [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——Maximum Depth of Binary Tree
LeetCode--Maximum Depth of Binary Tree Question Given a binary tree, find its maximum depth. The max ...
- 104. Maximum Depth of Binary Tree(C++)
104. Maximum Depth of Binary Tree Given a binary tree, find its maximum depth. The maximum depth is ...
- 【LeetCode练习题】Maximum Depth of Binary Tree
Maximum Depth of Binary Tree Given a binary tree, find its maximum depth. The maximum depth is the n ...
随机推荐
- mysql数据类型及存储过程
转自:http://www.cnblogs.com/mark-chan/p/5384139.html 存储过程简介 SQL语句需要先编译然后执行,而存储过程(Stored Procedure)是一组为 ...
- log buffer space事件(转)
看了这篇文章: Oracle常见的等待事件说明http://database.ctocio.com.cn/tips/38/6669538.shtml 对于Log Buffer Space-日志缓冲空间 ...
- 110道python题+理解(不断更新)
此篇题目在网上已经广为流传,但好多都不做解释,所以我想着自己一道一道的做一遍,并将相关涉及的做个补充,个人知识毕竟片面,有不足的地方还请大家多多指正 一.请用一行代码实现1-100之和 >> ...
- Linux系统下Nginx+PHP 环境安装配置
一.编译安装Nginx 官网:http://wiki.nginx.org/Install 下载:http://nginx.org/en/download.html # tar -zvxf nginx- ...
- javascript飞机大战-----007爆炸效果
要检验什么时候碰撞,我们必须了解什么时候不相撞.以上四种情况是不相撞的时候 首先在引擎里面写好什么时候碰撞什么时候不碰撞 /* 游戏引擎 */ var Engine = { //刚开始的游戏状态 ga ...
- centos7修改网卡名、密码重置
修改网卡名称 编辑 /etc/sysconfig/grub 倒数第二行quiet 后加入 net.ifnames=0 biosdevname=0 执行 grub2-mkconfig -o /bo ...
- Yii2如何批量添加数据
批量添加这个操作,在实际开发中经常用得到,今天小编抽空给大家整理些有关yii2批量添加的问题,感兴趣的朋友一起看看吧. 在上篇文章给大家介绍了关于浅析Yii2 gridview实现批量删除教程,当然, ...
- 【转】jQuery.ajax向后台传递数组问题
$.ajax({ url: "/xxx", type: "GET", data: { "boxIds": boxIds, "box ...
- Python3、Unicode、UTF-8、编码
text = u'你好,今天天气不错' text print(text) text = '\u4f60\u597d\uff0c\u4eca\u5929\u5929\u6c14\u4e0d\u9519' ...
- 少走冤枉路!带你走过SNMP的那些坑
SNMP(Simple Network Management Protocol)即简单网络管理协议,是在网络与系统监控领域中,最常使用的一种数据采集技术.尽管这个协议非常简单,但在大规模IT环境监测中 ...