内容简介: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
}
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Algorithm Design
Jon Kleinberg、Éva Tardos / Addison-Wesley / 2005-3-26 / USD 144.20
Algorithm Design introduces algorithms by looking at the real-world problems that motivate them. The book teaches students a range of design and analysis techniques for problems that arise in compu......一起来看看 《Algorithm Design》 这本书的介绍吧!