38 lines
603 B
Go
38 lines
603 B
Go
package main
|
|
|
|
type TreeNode struct {
|
|
Val int
|
|
Left *TreeNode
|
|
Right *TreeNode
|
|
}
|
|
|
|
/**
|
|
* Definition for a binary tree node.
|
|
* type TreeNode struct {
|
|
* Val int
|
|
* Left *TreeNode
|
|
* Right *TreeNode
|
|
* }
|
|
*/
|
|
func maxDepth(root *TreeNode) int {
|
|
depth := 0
|
|
queue := []*TreeNode{root}
|
|
if root == nil {
|
|
return 0
|
|
}
|
|
for len(queue) != 0 {
|
|
current := queue
|
|
queue = queue[len(queue):]
|
|
for _, node := range current {
|
|
if node.Left != nil {
|
|
queue = append(queue, node.Left)
|
|
}
|
|
if node.Right != nil {
|
|
queue = append(queue, node.Right)
|
|
}
|
|
}
|
|
depth++
|
|
}
|
|
return depth
|
|
}
|