当前位置:网站首页>【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)
}
边栏推荐
- The most powerful new household god card of Bank of communications. Apply to earn 2100 yuan. Hurry up if you haven't applied!
- The MySQL team development specifications used by various factories are too detailed. It is recommended to collect them!
- Binary tree traversal - middle order traversal (golang)
- Win: use shadow mode to view the Desktop Session of a remote user
- WCF: expose unset read-only DataMember property- WCF: Exposing readonly DataMember properties without set?
- MySQL backup and recovery + experiment
- Practice of tdengine in TCL air conditioning energy management platform
- 官宣!第三届云原生编程挑战赛正式启动!
- Go RPC call
- 如何做一个炫酷的墨水屏电子钟?
猜你喜欢
Visual explanation of Newton iteration method
Binary tree traversal - middle order traversal (golang)
Interesting practice of robot programming 15- autoavoidobstacles
如何搭建一支搞垮公司的技术团队?
A label colorful navigation bar
Subject 3 how to turn on the high beam diagram? Is the high beam of section 3 up or down
Interesting practice of robot programming 14 robot 3D simulation (gazebo+turtlebot3)
Missile interception -- UPC winter vacation training match
Exploration of short text analysis in the field of medical and health (II)
Win: use shadow mode to view the Desktop Session of a remote user
随机推荐
JVM - when multiple threads initialize the same class, only one thread is allowed to initialize
Practical case of SQL optimization: speed up your database
RichView TRVStyle MainRVStyle
Win: use PowerShell to check the strength of wireless signal
Pytorch fine tuning (Fortune): hollowed out design or cheating
Exploration of short text analysis in the field of medical and health (I)
MATLB | multi micro grid and distributed energy trading
力扣剑指offer——二叉树篇
Interesting practice of robot programming 16 synchronous positioning and map building (SLAM)
One plus six brushes into Kali nethunter
I use these six code comparison tools
Blue bridge - maximum common divisor and minimum common multiple
Tla+ through examples (XI) -- propositional logic and examples
Mysql database | build master-slave instances of mysql-8.0 or above based on docker
Codeforces Round #770 (Div. 2) ABC
What is the length of SHA512 hash string- What is the length of a hashed string with SHA512?
Flutter 2.10 update details
Bert fine tuning skills experiment
172. Zero after factorial
Why do you understand a16z? Those who prefer Web3.0 Privacy Infrastructure: nym