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 ...
随机推荐
- EUI组件之HScrollBar VScrollBar (动态设置滑块图片)
一.常规使用 官网教程里没有这个组件的使用方法 这个组件配合Scroller使用 拖动一个scroller到exml上.scroller上已经默认存在了HScrollBar和VScrollBar 当图 ...
- git add -A和git add . 的区别
git add -A和 git add . git add -u在功能上看似很相近,但还是有所差别. git add . :他会监控工作区的状态树,使用它会把工作时的所有变化提交到暂存区,包括文件内容 ...
- C++ string append方法的常用用法
append函数是向string的后面追加字符或字符串. 1).向string的后面加C-string string s = “hello “; const char *c = “out here “ ...
- C# WebBrowser的8个方法、13个属性和事件
1.方法 说明 GoBack 相当于IE的“后退”按钮,使你在当前历史列表中后退一项 GoForward 相当于IE的“前进”按钮,使你在当前历史列表中前进一项 GoHome 相当于IE的“主页”按 ...
- mysql中sql注入的随笔
当使用如下登录代码时:就会引发sql注入问题 怎么注入呢? 'or 1=1 # 就可以了. 为什么呢? 首先or:在sql中是或者,只要满足前一个或后一个条件即可,只要所以不论你是 'or 1=1 # ...
- 《码农周刊》干货精选--Python篇(转)
原文:http://baoz.me/446252 码农周刊,本人有修改 Python标准库,第三方库 按功能进行了分类,之前有一Pythoner说there is a library for ev ...
- laydate设置起始时间,laydate设置开始时间和结束时间
//设置开始时间 var startDate = laydate.render({ elem: '#start_date',//开始时间选择控件id min:'2018-6-1', type: 'da ...
- 【Loadrunner接口测试】什么情况需要区分PC和手机端去做压测?
1.PC和手机本身访问的都是接口,能有啥不一样的 这个一般看不出来,除非你们开发给APP的接口定义为http://api.mobile.com之类的 网站是网站,APP是APP但是不论是网站还是APP ...
- sdut AOE网上的关键路径(spfa+前向星)
http://acm.sdut.edu.cn/sdutoj/showproblem.php?pid=2498&cid=1304 题目描述 一个无环的有向图称为无环图(Directed Acyc ...
- testng日志和报告
TestNG是通过 Listeners 或者 Reporters 生成测试报告. Listeners,即 org.testng.ITestListener 的实现,能够在测试执行过程中发出各种测试结果 ...