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
}

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

查看所有标签

猜你喜欢:

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

WebWork in Action

WebWork in Action

Jason Carreira、Patrick Lightbody / Manning / 01 September, 2005 / $44.95

WebWork helps developers build well-designed applications quickly by creating re-usable, modular, web-based applications. "WebWork in Action" is the first book to focus entirely on WebWork. Like a tru......一起来看看 《WebWork in Action》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

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

Markdown 在线编辑器

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

HEX CMYK 互转工具