当前位置:网站首页>111. minimum depth of binary tree
111. minimum depth of binary tree
2022-06-11 14:23:00 【@[toc] (directory)】
https://leetcode-cn.com/problems/minimum-depth-of-binary-tree/

This inscription is written
/** * 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 minDepth(TreeNode root) {
// Determine the parameters of the recursive function
// Recursive termination condition
if(root == null){
return 0;
}
int leftH = minDepth(root.left);
int rightH = minDepth(root.right);
// If one of the left subtree or the right subtree is empty
// if(!(root.right==null)||!(root.left==null)){
// return 1+Math.max(leftH,rightH);
// }
if(root.left==null&&root.right!=null){
return 1+rightH;
}
if(root.left!=null&&root.right==null){
return 1+leftH;
}
// When the left and right subtrees are empty , That's the leaf node , The recursive termination condition is triggered ,
// return 0
return 1+Math.min(leftH,rightH); // The master , I understand ,
}
}


边栏推荐
- Lake Shore HR series sensors
- Airtest automated test
- Leetcode 1962. Remove stones to minimize the total amount (should be rounded up)
- How to manage the server to make the website stable and smooth
- VIM secondary replacement
- 高通WLAN框架学习(29)-- 6GHz 概述
- Zhu Ping: in the post epidemic era, medical beauty operations should not only be distracted, but also reverse the routine
- Solve the circular dependency bug. Relying upon circular references is discouraged and they are prohibited by default.
- [pyhton crawler] regular expression
- Distributed file system and enterprise application -- elk enterprise log analysis system
猜你喜欢

Powerful full text search tool anytxt searcher

Leetcode 1962. Remove stones to minimize the total amount (should be rounded up)

线程池的七个参数与拒绝策略

Leetcode 1968. Construct an array whose elements are not equal to the average value of two adjacent elements (yes, finally solved)
![[public class preview]: mxplayer Ott audio and video transcoding practice and optimization](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[public class preview]: mxplayer Ott audio and video transcoding practice and optimization

【Try to Hack】URL

Live800:智能客服提升客户体验的几种方式

Top 10 bone conduction earphones in the list, and five easy-to-use bone conduction earphones are recommended

Online "comment explicit" function, TME's wave point music cultivates music "private plots"

阿里一面,谈谈策略模式在项目中的使用
随机推荐
Leetcode 1968. Construct an array whose elements are not equal to the average value of two adjacent elements (yes, finally solved)
What is excess product power? Find the secret key of the second generation cs75plus in the year of the tiger
Easyexcel configuration and Application
Invalid bound statement (not found)错误【已解决】
Top 10 bone conduction earphones in the list, and five easy-to-use bone conduction earphones are recommended
Implementation of VGA protocol based on FPGA
Leetcode 1962. Remove stones to minimize the total amount (should be rounded up)
tp6基于whoops的异常接管(漂亮的界面)
In depth research and analysis report on global and Chinese diet food market
In depth research and analysis report on global and Chinese content audit market
基于Qt开发实现的任务管理器
Ali talked about the use of strategic mode in the project
使用cpolar远程办公(1)
基于FPGA的VGA协议实现
Current situation and future development trend of precision air conditioning market in the world and China
How to manually package your own projects
In depth research and analysis report on global and Chinese smart lamp Market
IC fresh Chinese cabbage price of 400000 yuan! Experienced experts who have worked for many years guide you how to choose an offer!
How can tampermonkey replace flash player with H5 player?
[public class preview]: mxplayer Ott audio and video transcoding practice and optimization