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 ...
随机推荐
- nodejs MySQL操作
一 wamp创建数据库 选择phpMyAdmin 选择用户,添加用户 填写数据库详细资料,填写完毕选择右下角的“执行” 用户添加成功 2. nodejs 安装mysql驱动 npm install ...
- Android 通过Socket 和服务器通讯
Extends:(http://www.cnblogs.com/likwo/p/3641135.html) Android 通过Socket 和服务器通讯,是一种比较常用的通讯方式,时间比较紧,说下大 ...
- nexus使用方法
转自:https://www.cnblogs.com/grey-wolf/p/6481166.html centos7搭建nexus maven私服 前置条件: 1.安装jdk,可参考 http: ...
- centos配置Tomcat以指定的身份(非root)运行
本文依赖的环境: 已安装并配置好jdk和tomcat环境 已安装并配置好gcc.make等编译工具 1.编译安装守护程序 cd /usr/local/tomcat7/bin/ tar vzxf c ...
- svn-maven-tomcat自动发布脚本
#!/bin/sh #svn-maven-tomcat自动发布脚本 #变量设置 svnpath=svn://10.60.10.120/研发部/xx-maven svnusername=xxx svnp ...
- vuls漏洞扫描工具
Vuls 是一款适用于 Linux/FreeBSD 的漏洞扫描程序,无代理,采用 Go 语言编写,对于系统管理员来说,每天必须执行安全漏洞分析和软件更新都是一个负担. 为避免生产环境宕机,系统管理员通 ...
- 170718、springboot编程之发送邮件
Spring提供了非常好用的JavaMailSender接口实现邮件发送.在Spring Boot的Starter模块中也为此提供了自动化配置.下面通过实例看看如何在Spring Boot中使用Jav ...
- Python开发【笔记】:如何在字典遍历中删除key值?
数据遍历时不能犯傻系列 前言: 针对字典做一些操作时,有时会遇到下面的状况,列如我们需要把data中的key值根据replace中的映射关系进行替换(Caller替换为caller) data = { ...
- 优云老王的心路历程(二):下一站Web体验监控产品
在上一篇文章中,和大家聊到了建立Web应用体验监控体系,经过了概念阶段,也完成了技术选型,就进入了把实质性的产品研发阶段.作为产品经理,时刻不敢忘记我们的产品目标:无限感知你的用户,建立完备的体验监控 ...
- Python并行编程(十三):进程池和mpi4py模块
1.基本概念 多进程库提供了Pool类来实现简单的多进程任务.Pool类有以下方法: - apply():直到得到结果之前一直阻塞. - apply_async():这是apply()方法的一个变体, ...