当前位置:网站首页>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;
}
}
边栏推荐
- PLC编程基础之数据类型、变量声明、全局变量和I/O映射(CODESYS篇 )
- Alibaba Tianchi SQL training camp task4 learning notes
- Arduino measures AC current
- 终于搞懂什么是动态规划的
- 【无标题】
- Global and Chinese markets for welding products 2022-2028: Research Report on technology, participants, trends, market size and share
- Solve the problem of "no input file specified" when ThinkPHP starts
- 基于STM32的ADC采样序列频谱分析
- 傅里叶分析概述
- Boring boring
猜你喜欢

2022 registration examination for safety management personnel of hazardous chemical business units and simulated reexamination examination for safety management personnel of hazardous chemical busines

从 1.5 开始搭建一个微服务框架——日志追踪 traceId

VOT toolkit environment configuration and use

Leetcode weekly The 280 game of the week is still difficult for the special game of the week's beauty team ~ simple simulation + hash parity count + sorting simulation traversal

LeetCode102. Sequence traversal of binary tree (output by layer and unified output)

One article deals with the microstructure and instructions of class

Registration and skills of hoisting machinery command examination in 2022
![[screen recording] how to record in the OBS area](/img/34/bd06bd74edcdabaf678c8d7385cae9.jpg)
[screen recording] how to record in the OBS area

Commonly used probability distributions: Bernoulli distribution, binomial distribution, polynomial distribution, Gaussian distribution, exponential distribution, Laplace distribution and Dirac delta d

Metasploit(msf)利用ms17_010(永恒之蓝)出现Encoding::UndefinedConversionError问题
随机推荐
Global and Chinese market of networked refrigerators 2022-2028: Research Report on technology, participants, trends, market size and share
两数之和、三数之和(排序+双指针)
Vcomp110.dll download -vcomp110 What if DLL is lost
基于STM32的ADC采样序列频谱分析
一文搞定class的微觀結構和指令
Function default parameters, function placeholder parameters, function overloading and precautions
PLC编程基础之数据类型、变量声明、全局变量和I/O映射(CODESYS篇 )
Masked Autoencoders Are Scalable Vision Learners (MAE)
Editor extensions in unity
VIM tail head intercept file import
实现反向代理客户端IP透传
抖音__ac_signature
Hcip day 11 (BGP agreement)
记录几个常见问题(202207)
3 find the greatest common divisor and the least common multiple
MCU case -int0 and INT1 interrupt count
audiopolicy
Matlab smooth curve connection scatter diagram
[untitled]
audiopolicy