当前位置:网站首页>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;
}
}
边栏推荐
- [digital signal denoising] improved wavelet modulus maxima digital signal denoising based on MATLAB [including Matlab source code 1710]
- Codeforces Global Round 19
- 【无标题】
- MCU case -int0 and INT1 interrupt count
- 终于搞懂什么是动态规划的
- Getting started stm32--gpio (running lantern) (nanny level)
- Vcomp110.dll download -vcomp110 What if DLL is lost
- Double pointer of linked list (fast and slow pointer, sequential pointer, head and tail pointer)
- Nanjing: full use of electronic contracts for commercial housing sales
- APK加固技术的演变,APK加固技术和不足之处
猜你喜欢

南京:全面启用商品房买卖电子合同

audiopolicy

One article deals with the microstructure and instructions of class

一文搞定class的微观结构和指令

Douban scoring applet Part-2

Hcip day 12 (BGP black hole, anti ring, configuration)

Double pointer of linked list (fast and slow pointer, sequential pointer, head and tail pointer)

Selenium+pytest automated test framework practice
![[speech processing] speech signal denoising based on Matlab GUI Hanning window fir notch filter [including Matlab source code 1711]](/img/03/8fa104b177698a15b7ffa70d4fb524.jpg)
[speech processing] speech signal denoising based on Matlab GUI Hanning window fir notch filter [including Matlab source code 1711]

Expectation, variance and covariance
随机推荐
Leetcode daily question 1189 The maximum number of "balloons" simple simulation questions~
二叉树(三)——堆排序优化、TOP K问题
Binary tree (III) -- heap sort optimization, top k problem
Global and Chinese markets of tantalum heat exchangers 2022-2028: Research Report on technology, participants, trends, market size and share
Error when LabVIEW opens Ni instance finder
Vcomp110.dll download -vcomp110 What if DLL is lost
Tensor attribute statistics
Business introduction of Zhengda international futures company
【无标题】
The method and principle of viewing the last modification time of the web page
[speech processing] speech signal denoising based on Matlab GUI Hanning window fir notch filter [including Matlab source code 1711]
513. Find the value in the lower left corner of the tree
【Note17】PECI(Platform Environment Control Interface)
Editor extensions in unity
Tiktok__ ac_ signature
我把开源项目alinesno-cloud-service关闭了
关于MySQL的30条优化技巧,超实用
Solve the problem of "no input file specified" when ThinkPHP starts
d3dx9_ What if 29.dll is missing? System missing d3dx9_ Solution of 29.dll file
Global and Chinese markets of industrial pH meters 2022-2028: Research Report on technology, participants, trends, market size and share