maximun-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.
此题类似二叉树最小深度。
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
import java.util.*;
public class Solution {
public int maxDepth(TreeNode root) {
if(root==null)
return 0;
if(root.left==null&&root.right==null)
return 1;
方法一:递归
/* if(root.left==null)
return maxDepth(root.right)+1;
if(root.right==null)
return maxDepth(root.left)+1;
int left=maxDepth(root.left);
int right=maxDepth(root.right);
return (left>right)?(left+1):(right+1);
*/
//方法二:层序遍历结束,每一层level加一
Queue<TreeNode> q=new LinkedList<>();
q.add(root);
int level=0;
while(!q.isEmpty()){
int size=q.size();
level++;
for(int i=0;i<size;i++){
TreeNode node=q.poll();
if(node.left!=null)
q.add(node.left);
if(node.right!=null)
q.add(node.right);
}
}
return level;
}
}
maximun-depth-of-binary-tree的更多相关文章
- 【LeetCode练习题】Minimum 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 二叉树的最小深度
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- [LeetCode] Maximum Depth of Binary Tree 二叉树的最大深度
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- [LintCode] 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][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 —— binary tree [Same Tree] && [Maximum Depth of Binary Tree]
Same Tree Given two binary trees, write a function to check if they are equal or not. Two binary tre ...
- 33. Minimum Depth of Binary Tree && Balanced Binary Tree && Maximum Depth of Binary Tree
Minimum Depth of Binary Tree OJ: https://oj.leetcode.com/problems/minimum-depth-of-binary-tree/ Give ...
- Leetcode 111 Minimum Depth of Binary Tree 二叉树
找出最短的从叶子到根的路径长 可以回忆Maximum Depth of Binary Tree的写法,只不过在!root,我把它改成了10000000,还有max函数改成了min函数,最后的值如果是1 ...
- 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 ...
随机推荐
- Oracle使用游标查询指定数据表的所有字段名称组合而成的字符串
应用场合:参考网上查询数据表的所有字段名代码,使用游标生成指定单个表的所有字段名跟逗号组成的用于select 逗号隔开的字段名列表 from字符串等场合. 查询结果输出如下: 当前数据表TB_UD_ ...
- Spring中Bean多种实现切换方案
一个公共工程中的Spring配置文件,可能会被多个工程引用.因为每个工程可能只需要公共工程中的一部分Bean,所以这些工程的Spring容器启动时,需要区分开哪些Bean要创建出来.另一种场景是:想通 ...
- String类用法总结
String类在编程中出现的频率是非常高的,熟练掌握是很有必要的 一.常用方法总结: 获取方法 1.1:字符串中包含的字符数,也就是字符串的长度. int length():获取长度 1.2:根据位置 ...
- MMD4Mecanim介绍
MMD4Mecanim是一位11区大神写的为Unity游戏引擎导入MMD模型的插件,目前依然在持续更新中. 需要Unity4.0以上版本.本教程使用Unity4.6.1(下载请自行百度) 插件君首页: ...
- [C++学习历程]基础部分 C++中的类型和声明
前面搭起了C++的VS环境,可以在VS中编写C++代码了,也运行了最简单的一个程序Helloworld.那么我们该怎么才能写出功能强大的程序,怎样才能随心所欲的应用呢,那就需要重新回头来,从C++基础 ...
- Android(Lollipop/5.0) Material Design(二) 入门指南
Material Design系列 Android(Lollipop/5.0)Material Design(一) 简介 Android(Lollipop/5.0)Material Design(二) ...
- 读书笔记 - reword (重来)
reword (重来) 虽然我是一个不是很喜欢看书的人,但是公认的是看书对提高个人的水平是很有帮助的. 而且我想,如果我要写一本书,我一定会经过多次校验.经过长时间思考确保无误后才会出版的.所以我想看 ...
- shell脚本整段注释
摘自:http://zhidao.baidu.com/link?url=XmCCZmfluRe6n8TjPRKJTx4GGOUPSGX1VNBm-euqGdpKGpveTESxC0HL90UBNT5n ...
- Linux进程实践(2) --僵尸进程与文件共享
孤儿进程与僵尸进程 孤儿进程: 如果父进程先退出,子进程还没退出那么子进程的父进程将变为init进程.(注:任何一个进程都必须有父进程) //生成孤儿进程 int main(int argc, cha ...
- 【嵌入式开发】向开发板中烧写Linux系统-型号S3C6410
作者 : 万境绝尘 转载请著名出处 终于拿到板子了, 嵌入式开发正式开启. 板子型号 : 三星 S3C6410 基于ARM11, 指令集基于arm6指令集; 为毛不是 Cortext A9的板子; 烧 ...