package main
import . "nc_tools"

/*
//递归
func maxDepth( root *TreeNode ) int {
    if root == nil {
        return 0
    }
    return max(maxDepth(root.Left), maxDepth(root.Right)) +1
}

func max(x, y int ) int {
    if x > y {
        return x
    }
    return y
}
*/

//迭代
func maxDepth(root *TreeNode) int {
    if root == nil {
        return 0
    }

    queue := []*TreeNode{root}
    res := 0

    for len(queue) > 0 {
        n := len(queue)

        for i := 0; i < n; i++ {
            node := queue[0]
            queue = queue[1:]

            if node.Left != nil {
                queue = append(queue, node.Left)
            }
            if node.Right != nil {
                queue = append(queue, node.Right)
            }
        }
        res++
    }
    return res
}