当前位置:网站首页>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;
}
}
边栏推荐
- 查看网页最后修改时间方法以及原理简介
- 2022.02.13 - SX10-30. Home raiding II
- Arduino measures AC current
- 2.13 summary
- Marginal probability and conditional probability
- 谷歌地图案例
- 分布式解决方案之TCC
- [screen recording] how to record in the OBS area
- Thoroughly understand JVM class loading subsystem
- One article deals with the microstructure and instructions of class
猜你喜欢
终于搞懂什么是动态规划的
d3dx9_ What if 29.dll is missing? System missing d3dx9_ Solution of 29.dll file
Yiwen gets rid of the garbage collector
我把开源项目alinesno-cloud-service关闭了
【Note17】PECI(Platform Environment Control Interface)
【无标题】
SPSS analysis of employment problems of college graduates
Unity Max and min constraint adjustment
Expectation, variance and covariance
Usage Summary of scriptable object in unity
随机推荐
Unity Max and min constraint adjustment
Fix the memory structure of JVM in one article
从 1.5 开始搭建一个微服务框架——日志追踪 traceId
Negative sampling
How can easycvr cluster deployment solve the massive video access and concurrency requirements in the project?
30 optimization skills about mysql, super practical
Common model making instructions
Metasploit (MSF) uses MS17_ 010 (eternal blue) encoding:: undefined conversionerror problem
[speech processing] speech signal denoising and denoising based on Matlab GUI low-pass filter [including Matlab source code 1708]
First, redis summarizes the installation types
Paddle Serving v0.9.0 重磅发布多机多卡分布式推理框架
[speech processing] speech signal denoising based on Matlab GUI Hanning window fir notch filter [including Matlab source code 1711]
audiopolicy
Usage Summary of scriptable object in unity
谷歌地图案例
Metasploit(msf)利用ms17_010(永恒之蓝)出现Encoding::UndefinedConversionError问题
抖音__ac_signature
VIM tail head intercept file import
SPSS analysis of employment problems of college graduates
Metasploit(msf)利用ms17_010(永恒之蓝)出现Encoding::UndefinedConversionError问题