当前位置:网站首页>[leetcode] maximum depth of binary tree
[leetcode] maximum depth of binary tree
2022-07-28 22:19:00 【ajjjjjjjjjtty】
104. The maximum depth of a binary tree
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 7Return to its maximum depth 3 .
Method 1 : Depth-first search
Ideas and algorithms
If we know the maximum depth of the left and right subtrees ll and rr, Then the maximum depth of the binary tree is
max(l,r)+1
The maximum depth of left and right subtrees can be calculated in the same way . So we can use 「 Depth-first search 」 To calculate the maximum depth of the binary tree . To be specific , When calculating the maximum depth of the current binary tree , You can recursively calculate the maximum depth of its left and right subtrees , And then in O(1)O(1) Calculate the maximum depth of the current binary tree in time . Recursion exits when an empty node is accessed .
/**
* 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 {
public int maxDepth(TreeNode root) {
if(root == null){
return 0;
}else{
int leftheight = maxDepth(root.left);
int rightheight = maxDepth(root.right);
return Math.max(leftheight, rightheight) + 1;
}
}
}边栏推荐
- Use pl/sql
- [NLP] generate word cloud
- 学习 Kotlin - 扩展函数
- 2021 mathematical modeling group B exercise
- II. Explanation of the sequence and deserialization mechanism of redistemplate
- Desai wisdom number - line chart (stacking area chart): ranking of deposits of different occupational groups in the proportion of monthly income in 2022
- Make trouble fishing day by day
- ESP8266-Arduino编程实例-定时器与中断
- HCIP(13)
- hcip实验(12)
猜你喜欢

Gateway technology of IOT technology stack

HCIP(14)

记录Flutter解决A RenderFlex overflowed by 7.3 pixels on the bottom溢出问题

Learning notes and summary of C language programming specification

JS DOM编程之平平无奇小练习

DHCP and PPPoE protocols and packet capture analysis

Add DNS server to LAN for domain name resolution

科大讯飞笔试

HCIP(8)

SQL注入 Less38(堆叠注入)
随机推荐
Oracle triggers
表单验证和级联下拉列表(多种实现)
Static route and default route experiment
PCB材料简单介绍
HCIP(13)
SQL injection less38 (Stack Injection)
LCR测试仪最为主要的功能和用途都是什么
array_ diff_ The method of not comparing array values when Assoc element is an array
IFLYTEK written examination
熊市下 DeFi 的未来趋势
HCIP(12)
vuejs中如何实现动态路由切换及路由的缓存
Form validation and cascading drop-down lists (multiple implementations)
【云原生之kubernetes】在kubernetes集群下的映射外部服务—Eendpoint
The applet listens for the target node to appear on the screen
II. Explanation of the sequence and deserialization mechanism of redistemplate
Record the fluent to solve the problem of a renderflex overflowed by 7.3 pixels on the bottom
[NLP] generate word cloud
lotus 1.16.0 延长扇区过期时间
Hcip experiment (15)