美文网首页
golang 语言 LeedCode104 二叉树的最大深度

golang 语言 LeedCode104 二叉树的最大深度

作者: 稻田夕阳 | 来源:发表于2018-12-19 10:10 被阅读0次

golang 语言 LeedCode104 二叉树的最大深度

func maxDepth(root *TreeNode) int {
   if root==nil {
       return 0
   }
   if root.Left==nil && root.Right ==nil{
       return 1
   }
   
   i := depth(root)
   return i
}

func depth(root *TreeNode) int {
   queue := list.New()
   queue.PushBack(root)
   var maxDeep int=0
   for{
       len := queue.Len()
       if len== 0 {
           break
       }
       for i:=0;i<len ;i++  {
           front := queue.Front()
           node := (front.Value).(*TreeNode)
           queue.Remove(front)
           if node.Left!=nil {
               queue.PushBack(node.Left)
           }
           if node.Right!=nil {
               queue.PushBack(node.Right)
           }
       }
       maxDeep++
   }
   return maxDeep
}

相关文章

网友评论

      本文标题:golang 语言 LeedCode104 二叉树的最大深度

      本文链接:https://www.haomeiwen.com/subject/qrpdkqtx.html