mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-07 07:35:35 +08:00
Go
This commit is contained in:
25
添加0222.完全二叉树的节点个数Go版本.md
Normal file
25
添加0222.完全二叉树的节点个数Go版本.md
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
```go
|
||||||
|
func countNodes(root *TreeNode) int {
|
||||||
|
if root == nil {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
q := list.New()
|
||||||
|
q.PushBack(root)
|
||||||
|
res := 0
|
||||||
|
for q.Len() > 0 {
|
||||||
|
n := q.Len()
|
||||||
|
for i := 0; i < n; i++ {
|
||||||
|
node := q.Remove(q.Front()).(*TreeNode)
|
||||||
|
if node.Left != nil {
|
||||||
|
q.PushBack(node.Left)
|
||||||
|
}
|
||||||
|
if node.Right != nil {
|
||||||
|
q.PushBack(node.Right)
|
||||||
|
}
|
||||||
|
res++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
22
添加559.n叉树的最大深度Go版本.md
Normal file
22
添加559.n叉树的最大深度Go版本.md
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
```go
|
||||||
|
func maxDepth(root *Node) int {
|
||||||
|
if root == nil {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
q := list.New()
|
||||||
|
q.PushBack(root)
|
||||||
|
depth := 0
|
||||||
|
for q.Len() > 0 {
|
||||||
|
n := q.Len()
|
||||||
|
for i := 0; i < n; i++ {
|
||||||
|
node := q.Remove(q.Front()).(*Node)
|
||||||
|
for j := range node.Children {
|
||||||
|
q.PushBack(node.Children[j])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
depth++
|
||||||
|
}
|
||||||
|
return depth
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
Reference in New Issue
Block a user