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

栏目: Go · 发布时间: 6年前

内容简介:golang 语言 LeedCode104 二叉树的最大深度

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
}

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Algorithms in Java, Part 5

Algorithms in Java, Part 5

Robert Sedgewick / Addison-Wesley Professional / 2003-7-25 / USD 54.99

Algorithms in Java, Third Edition, Part 5: Graph Algorithms is the second book in Sedgewick's thoroughly revised and rewritten series. The first book, Parts 1-4, addresses fundamental algorithms, data......一起来看看 《Algorithms in Java, Part 5》 这本书的介绍吧!

URL 编码/解码
URL 编码/解码

URL 编码/解码

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具