当前位置:网站首页>[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;
}
}
}边栏推荐
- LCR测试仪最为主要的功能和用途都是什么
- hcip实验(12)
- Esp8266 Arduino programming example - timer and interrupt
- Necessary for in-depth learning: split the data set, split the labels according to the split pictures, and check the interval of all marked labels
- Ecmasript 5/6 notes
- Esp8266 Arduino programming example - SPIFs and data upload (Arduino IDE and platformio IDE)
- 【二叉树】二叉树中的伪回文路径
- SSH password free login
- Part 8: creating camera classes
- Openresty request authentication
猜你喜欢

Byte side: can TCP and UDP use the same port?

Have you seen the management area decoupling architecture? Can help customers solve big problems

Chapter 7: drawing rotating cubes
![[LiteratureReview]Object Detection and Mapping with Bounding Box Constraints](/img/37/7cb5fa3a9078a5f5947485147c819d.png)
[LiteratureReview]Object Detection and Mapping with Bounding Box Constraints

DHCP和PPPoE协议以及抓包分析

【机器学习】朴素贝叶斯对文本分类--对人名国别分类

成立不到一年!MIT衍生量子计算公司完成900万美元融资

Record the fluent to solve the problem of a renderflex overflowed by 7.3 pixels on the bottom

Kubevera plug-in addons download address
![[NLP] generate word cloud](/img/c4/4e9707bba58732a90d1c30312719a3.png)
[NLP] generate word cloud
随机推荐
数据可视化新闻,不一样的新闻报道形式
第 8 篇:创建摄像机类
Brief introduction to PCB materials
Typeof principle
typeof原理
Bugku, Web: all filtered
HCIP(10)
HCIP(14)
vuejs中如何实现动态路由切换及路由的缓存
II. Explanation of the sequence and deserialization mechanism of redistemplate
[binary tree] pseudo palindrome path in binary tree
【云原生之kubernetes】在kubernetes集群下的映射外部服务—Eendpoint
Summary of the use of hash table set and map when leetcode brushes questions
SQL注入 Less42(POST型堆叠注入)
什么是时间复杂度
Learn kotlin - extension function
HCIP(8)
Getting started with Oracle
ECMASript 5/6 笔记
熊市下 DeFi 的未来趋势