当前位置:网站首页>【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)
}
边栏推荐
- Change the background color of a pop-up dialog
- A label making navigation bar
- Codeforces Global Round 19 ABC
- pytorch fine-tuning (funtune) : 镂空设计or 偷梁换柱
- R language uses logistic regression and afrima, ARIMA time series models to predict world population
- Android advanced interview question record in 2022
- Outlook: always prompt for user password
- The application and Optimization Practice of redis in vivo push platform is transferred to the end of metadata by
- [understanding of opportunity -38]: Guiguzi - Chapter 5 flying clamp - warning one: there is a kind of killing called "killing"
- 85.4% mIOU! NVIDIA: using multi-scale attention for semantic segmentation, the code is open source!
猜你喜欢
[source code attached] Intelligent Recommendation System Based on knowledge map -sylvie rabbit
如何搭建一支搞垮公司的技術團隊?
Open source SPL optimized report application coping endlessly
Write a thread pool by hand, and take you to learn the implementation principle of ThreadPoolExecutor thread pool
JVM's responsibility - load and run bytecode
PowerShell: use PowerShell behind the proxy server
Grub 2.12 will be released this year to continue to improve boot security
Missile interception -- UPC winter vacation training match
Go RPC call
PowerShell:在代理服务器后面使用 PowerShell
随机推荐
JVM - when multiple threads initialize the same class, only one thread is allowed to initialize
JVM's responsibility - load and run bytecode
Chinese natural language processing, medical, legal and other public data sets, sorting and sharing
Rabbit MQ message sending of vertx
[technology development-26]: data security of new information and communication networks
Yolov5 model training and detection
PowerShell:在代理服务器后面使用 PowerShell
Some query constructors in laravel (2)
RichView TRVStyle MainRVStyle
What is the length of SHA512 hash string- What is the length of a hashed string with SHA512?
The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
One click generation and conversion of markdown directory to word format
Limited query of common SQL operations
Erreur de type de datagramme MySQL en utilisant Druid
Which common ports should the server open
runc hang 导致 Kubernetes 节点 NotReady
Advanced learning of MySQL -- Application -- Introduction
Using druid to connect to MySQL database reports the wrong type
Outlook:总是提示输入用户密码
Include rake tasks in Gems - including rake tasks in gems