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 ...
随机推荐
- 【BZOJ2300】[HAOI2011]防线修建 set维护凸包
[BZOJ2300][HAOI2011]防线修建 Description 近来A国和B国的矛盾激化,为了预防不测,A国准备修建一条长长的防线,当然修建防线的话,肯定要把需要保护的城市修在防线内部了.可 ...
- Android dialog 全屏
Android中让Dialog全屏: 一.在style中定义样式: <?xml version="1.0" encoding="utf-8"?> & ...
- DGbroker快速失败转移
1.先决条件 DGMGRL> ENABLE FAST_START FAILOVER; Error: ORA-: requirements not met for enabling fast-st ...
- postgresql----数据库表约束----UNIQUE
四.UNIQUE ---- 唯一约束 唯一键可以是单个字段,也可以是多个字段的组合,设置唯一约束后,INSERT或UPDATE时如果表中唯一键字段中已存在该数据,则拒绝该行数据的INSERT或UPDA ...
- pandas的drop函数
当你要删除某一行或者某一列时,用drop函数,它不改变原有的df中的数据,而是返回另一个dataframe来存放删除后的数据. 1.命令: df.drop() 删除行:df.drop('apps') ...
- Go Configure Support hot reloading.
Go Configure – Josh Betz https://josh.blog/2017/04/go-configure Go Configure APRIL 27, 2017 # DEVELO ...
- UA-* headers
HTTP The Definitive Guide Request headers are headers that make sense only in a request message. The ...
- 双态运维联盟(BOA)正式成立
3月1日,由联想.新华三.华为等12家IT企业在北京正式达成协议,联合发起成立“双态运维联盟”.中国电子工业标准技术协会.信息技术服务分会数据中心运营管理工作组(DCMG)组长肖建一先生出席了会议. ...
- python知识大全目录,想学的看过来!
Python总结篇——知识大全 python装饰器 PyCharm安装与配置,python的Hello World sort与sorted的区别及实例 我必须得告诉大家的MySQL优化 ...
- Scrapy框架(3)
一.如何提升scrapy框架的爬取效率 增加并发: 默认scrapy开启的并发线程为32个,可以适当进行增加.在settings配置文件中修改CONCURRENT_REQUESTS = 100,并发设 ...