当前位置:网站首页>【LeetCode】111. Minimum depth of binary tree (2 brushes of wrong questions)
【LeetCode】111. Minimum depth of binary tree (2 brushes of wrong questions)
2022-07-05 02:20:00 【Kaimar】


- Ideas
Want to use queues , Use the method of sequence traversal to judge , Once there is a leaf node, exit .( Iterative method )
Refer to the explanation of the question
Using recursive method , First, clarify the three elements of recursion
Recursive parameters and return values : The parameter is the current node , The return value is the minimum depth of the current node as the root node ;
The termination condition of recursion : When there is a leaf node, it terminates ;
Recursive single-layer logic : First, specify what traversal method to use , Here we use post order traversal ( Because to compare Recursively returns Later results ); secondly , Use the smaller depth value in the left and right subtrees .
// Iterative method
/** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */
func minDepth(root *TreeNode) int {
if root == nil {
return 0
}
queue := []*TreeNode{
root}
res := 0
for len(queue) > 0 {
curLen := len(queue)
res++
for i := 0; i < curLen; i++ {
// Traverse the current layer
node := queue[0]
queue = queue[1:]
if node.Left != nil {
queue = append(queue, node.Left)
}
if node.Right != nil {
queue = append(queue, node.Right)
}
if node.Left == nil && node.Right == nil {
return res
}
}
}
return res
}
// Recursive method
func min(a, b int) int {
if a > b {
return b
}
return a
}
func minDepth(root *TreeNode) int {
if root == nil {
return 0
}
// Left
left := minDepth(root.Left)
// Right
right := minDepth(root.Right)
// in
// One of the two cases is empty , What needs to be returned is the minimum depth on the side that is not empty
if root.Left != nil && root.Right == nil {
return 1 + left
}
if root.Left == nil && root.Right != nil {
return 1 + right
}
// All is empty
return 1 + min(left, right)
}

边栏推荐
- Word processing software
- Data guard -- theoretical explanation (III)
- Security level
- Talk about the things that must be paid attention to when interviewing programmers
- Some query constructors in laravel (2)
- Tucson will lose more than $400million in the next year
- Richview trvunits image display units
- Grpc message sending of vertx
- JVM - when multiple threads initialize the same class, only one thread is allowed to initialize
- ICSI 311 Parser
猜你喜欢

Yyds dry inventory swagger positioning problem ⽅ formula

Do you know the eight signs of a team becoming agile?

Go RPC call

Android advanced interview question record in 2022

Icu4c 70 source code download and compilation (win10, vs2022)

Practice of tdengine in TCL air conditioning energy management platform

Runc hang causes the kubernetes node notready

Subject 3 how to turn on the high beam diagram? Is the high beam of section 3 up or down

Introduce reflow & repaint, and how to optimize it?

Stored procedure and stored function in Oracle
随机推荐
Missile interception -- UPC winter vacation training match
Win: enable and disable USB drives using group policy
Application and Optimization Practice of redis in vivo push platform
Yyds dry inventory swagger positioning problem ⽅ formula
Include rake tasks in Gems - including rake tasks in gems
STL container
Binary tree traversal - middle order traversal (golang)
[download white paper] does your customer relationship management (CRM) really "manage" customers?
Exploration of short text analysis in the field of medical and health (II)
A label colorful navigation bar
The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
WCF: expose unset read-only DataMember property- WCF: Exposing readonly DataMember properties without set?
Mysql database | build master-slave instances of mysql-8.0 or above based on docker
85.4% mIOU! NVIDIA: using multi-scale attention for semantic segmentation, the code is open source!
Li Kou Jianzhi offer -- binary tree chapter
Introduce reflow & repaint, and how to optimize it?
Prometheus monitors the correct posture of redis cluster
Redis distributed lock, lock code logic
Win: add general users to the local admins group
Pytorch common code snippet collection