mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-10 04:06:51 +08:00
添加 111.二叉树的最小深度 go版本
This commit is contained in:
@ -301,6 +301,64 @@ class Solution:
|
|||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
|
||||||
|
```go
|
||||||
|
/**
|
||||||
|
* Definition for a binary tree node.
|
||||||
|
* type TreeNode struct {
|
||||||
|
* Val int
|
||||||
|
* Left *TreeNode
|
||||||
|
* Right *TreeNode
|
||||||
|
* }
|
||||||
|
*/
|
||||||
|
func min(a, b int) int {
|
||||||
|
if a < b {
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
return b;
|
||||||
|
}
|
||||||
|
// 递归
|
||||||
|
func minDepth(root *TreeNode) int {
|
||||||
|
if root == nil {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if root.Left == nil && root.Right != nil {
|
||||||
|
return 1 + minDepth(root.Right);
|
||||||
|
}
|
||||||
|
if root.Right == nil && root.Left != nil {
|
||||||
|
return 1 + minDepth(root.Left);
|
||||||
|
}
|
||||||
|
return min(minDepth(root.Left), minDepth(root.Right)) + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 迭代
|
||||||
|
|
||||||
|
func minDepth(root *TreeNode) int {
|
||||||
|
dep := 0;
|
||||||
|
queue := make([]*TreeNode, 0);
|
||||||
|
if root != nil {
|
||||||
|
queue = append(queue, root);
|
||||||
|
}
|
||||||
|
for l := len(queue); l > 0; {
|
||||||
|
dep++;
|
||||||
|
for ; l > 0; l-- {
|
||||||
|
node := queue[0];
|
||||||
|
if node.Left == nil && node.Right == nil {
|
||||||
|
return dep;
|
||||||
|
}
|
||||||
|
if node.Left != nil {
|
||||||
|
queue = append(queue, node.Left);
|
||||||
|
}
|
||||||
|
if node.Right != nil {
|
||||||
|
queue = append(queue, node.Right);
|
||||||
|
}
|
||||||
|
queue = queue[1:];
|
||||||
|
}
|
||||||
|
l = len(queue);
|
||||||
|
}
|
||||||
|
return dep;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
JavaScript:
|
JavaScript:
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user