当前位置:网站首页>【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)
}
边栏推荐
- [uc/os-iii] chapter 1.2.3.4 understanding RTOS
- Android advanced interview question record in 2022
- Marubeni Baidu applet detailed configuration tutorial, approved.
- Stored procedure and stored function in Oracle
- Talk about the things that must be paid attention to when interviewing programmers
- Official announcement! The third cloud native programming challenge is officially launched!
- Unified blog writing environment
- Summary and practice of knowledge map construction technology
- Codeforces Round #770 (Div. 2) ABC
- Luo Gu Pardon prisoners of war
猜你喜欢
Open source SPL optimized report application coping endlessly
Chinese natural language processing, medical, legal and other public data sets, sorting and sharing
openresty ngx_lua执行阶段
Official announcement! The third cloud native programming challenge is officially launched!
Subject 3 how to turn on the high beam diagram? Is the high beam of section 3 up or down
Three properties that a good homomorphic encryption should satisfy
Practical case of SQL optimization: speed up your database
Prometheus monitors the correct posture of redis cluster
Comment mettre en place une équipe technique pour détruire l'entreprise?
One plus six brushes into Kali nethunter
随机推荐
179. Maximum number - sort
One plus six brushes into Kali nethunter
Write a thread pool by hand, and take you to learn the implementation principle of ThreadPoolExecutor thread pool
Asynchronous and promise
Runc hang causes the kubernetes node notready
Do you know the eight signs of a team becoming agile?
Stored procedure and stored function in Oracle
Luo Gu Pardon prisoners of war
Five ways to query MySQL field comments!
[机缘参悟-38]:鬼谷子-第五飞箝篇 - 警示之一:有一种杀称为“捧杀”
How to make a cool ink screen electronic clock?
Richview trvunits image display units
Tla+ through examples (XI) -- propositional logic and examples
Huawei machine test question: longest continuous subsequence
Marubeni Baidu applet detailed configuration tutorial, approved.
100 basic multiple choice questions of C language (with answers) 04
[Digital IC hand tearing code] Verilog edge detection circuit (rising edge, falling edge, double edge) | topic | principle | design | simulation
官宣!第三届云原生编程挑战赛正式启动!
Interesting practice of robot programming 16 synchronous positioning and map building (SLAM)
力扣剑指offer——二叉树篇