leet-code/maximum-depth-of-binary-tree/sol.go

38 lines
603 B
Go
Raw Normal View History

2022-06-09 14:45:14 +00:00
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
}