题解 | #二叉树的深度#
二叉树的深度
http://www.nowcoder.com/practice/435fb86331474282a3499955f0a41e8b
package main
import . "nc_tools"
/*
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func TreeDepth(root *TreeNode ) int {
if root == nil {
return 0
}
queue := []*TreeNode{}
queue = append(queue, root)
var depth int
for len(queue) > 0 {
size := len(queue)
for size > 0 {
//custom queue
cur := queue[0]
queue = queue[1:]
if cur.Left != nil {
queue = append(queue, cur.Left)
}
if cur.Right != nil {
queue = append(queue, cur.Right)
}
size--
}
depth++
}
return depth
}