当前位置:网站首页>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;
}
}
边栏推荐
- 二叉树(二)——堆的代码实现
- openresty ngx_lua正則錶達式
- 实现反向代理客户端IP透传
- Binary tree (II) -- code implementation of heap
- PLC编程基础之数据类型、变量声明、全局变量和I/O映射(CODESYS篇 )
- audiopolicy
- Global and Chinese markets of industrial pH meters 2022-2028: Research Report on technology, participants, trends, market size and share
- 第一讲:蛇形矩阵
- Paddle Serving v0.9.0 重磅发布多机多卡分布式推理框架
- Boring boring
猜你喜欢
Thoroughly understand JVM class loading subsystem
Activate function and its gradient
How can easycvr cluster deployment solve the massive video access and concurrency requirements in the project?
Usage Summary of scriptable object in unity
Error when LabVIEW opens Ni instance finder
Tensor attribute statistics
Three.js-01 入门
Registration and skills of hoisting machinery command examination in 2022
傅里叶分析概述
Editor extensions in unity
随机推荐
Global and Chinese market of diesel fire pump 2022-2028: Research Report on technology, participants, trends, market size and share
LeetCode145. Post order traversal of binary tree (three methods of recursion and iteration)
Metasploit(msf)利用ms17_010(永恒之蓝)出现Encoding::UndefinedConversionError问题
Hcip day 12 (BGP black hole, anti ring, configuration)
Usage Summary of scriptable object in unity
Global and Chinese markets of industrial pH meters 2022-2028: Research Report on technology, participants, trends, market size and share
Arduino 测量交流电流
第十七周作业
openresty ngx_ Lua request response
数据库基础知识(面试)
audiopolicy
Spectrum analysis of ADC sampling sequence based on stm32
VOT toolkit environment configuration and use
Double pointer of linked list (fast and slow pointer, sequential pointer, head and tail pointer)
SPSS analysis of employment problems of college graduates
npm ELECTRON_ Mirror is set as domestic source (npmmirror China mirror)
Error when LabVIEW opens Ni instance finder
查看网页最后修改时间方法以及原理简介
[speech processing] speech signal denoising and denoising based on Matlab GUI low-pass filter [including Matlab source code 1708]
2022 G3 boiler water treatment simulation examination and G3 boiler water treatment simulation examination question bank