当前位置:网站首页>February 13, 2022 -5- maximum depth of binary tree
February 13, 2022 -5- maximum depth of binary tree
2022-07-05 23:01:00 【Procedural ape does not lose hair 2】
Given a binary tree , Find out the maximum depth .
The depth of a binary tree is the number of nodes in the longest path from the root node to the farthest leaf node .
explain : A leaf node is a node that has no children .
Example :
Given binary tree [3,9,20,null,null,15,7],
3
/
9 20
/
15 7
Return to its maximum depth 3 .
java Code :
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
// Method 1 : Depth first
// public int maxDepth(TreeNode root) {
// if(root == null) {
// return 0;
// }
// int leftDepth = maxDepth(root.left);
// int rightDepth = maxDepth(root.right);
// return Math.max(leftDepth, rightDepth) +1 ;
// }
// Method 2 : breadth-first
public int maxDepth(TreeNode root) {
if(root == null) {
return 0;
}
Queue<TreeNode> queue = new LinkedList<TreeNode>();
queue.offer(root);
int ans = 0;
while(!queue.isEmpty()) {
int size = queue.size();
while(size>0) {
TreeNode tree = queue.poll();
if(tree.left != null) {
queue.offer(tree.left);
}
if(tree.right != null) {
queue.offer(tree.right);
}
size--;
}
ans++;
}
return ans;
}
}
边栏推荐
- Masked Autoencoders Are Scalable Vision Learners (MAE)
- 二叉树(三)——堆排序优化、TOP K问题
- Global and Chinese market of diesel fire pump 2022-2028: Research Report on technology, participants, trends, market size and share
- Simple and beautiful method of PPT color matching
- My experience and summary of the new Zhongtai model
- Error when LabVIEW opens Ni instance finder
- Starting from 1.5, build a micro Service Framework -- log tracking traceid
- Three.JS VR看房
- Vcomp110.dll download -vcomp110 What if DLL is lost
- Binary tree (III) -- heap sort optimization, top k problem
猜你喜欢
Business introduction of Zhengda international futures company
How can easycvr cluster deployment solve the massive video access and concurrency requirements in the project?
Selenium+Pytest自动化测试框架实战
[untitled]
[secretly kill little buddy pytorch20 days] - [Day2] - [example of picture data modeling process]
谷歌地图案例
Arduino measures AC current
First, redis summarizes the installation types
第一讲:蛇形矩阵
链表之双指针(快慢指针,先后指针,首尾指针)
随机推荐
Hcip day 12 (BGP black hole, anti ring, configuration)
openresty ngx_lua请求响应
一文搞定JVM的内存结构
Unity Max and min constraint adjustment
Evolution of APK reinforcement technology, APK reinforcement technology and shortcomings
Arduino measures AC current
Common JVM tools and optimization strategies
openresty ngx_ Lua request response
Global and Chinese market of water treatment technology 2022-2028: Research Report on technology, participants, trends, market size and share
Binary tree (III) -- heap sort optimization, top k problem
PLC编程基础之数据类型、变量声明、全局变量和I/O映射(CODESYS篇 )
Yiwen gets rid of the garbage collector
TypeError: this. getOptions is not a function
Ultrasonic sensor flash | LEGO eV3 Teaching
二叉树(二)——堆的代码实现
如何快速理解复杂业务,系统思考问题?
Vcomp110.dll download -vcomp110 What if DLL is lost
Metasploit(msf)利用ms17_010(永恒之蓝)出现Encoding::UndefinedConversionError问题
Global and Chinese markets of tantalum heat exchangers 2022-2028: Research Report on technology, participants, trends, market size and share
The difference between MVVM and MVC