2 回答

TA贡献1827条经验 获得超8个赞
紧急错误是由线路引起的
qu.Remove(qu.Front())
因为元素传递给了 qu。Remove()
是,发生这种情况是因为 qu.Front()
在列表为空时返回(即删除所有元素后)nil
nil
循环条件
for qu != nil {
是错误的,因为它永远不会被满足,因为它永远不会被满足。qu
nil
循环应该“直到列表不为空”,即:
for qu.Len() > 0 {
这将防止返回,进而将其传递给并导致紧急错误。qu.Front()
nil
qu.Remove()

TA贡献1775条经验 获得超11个赞
func (t *bt) topview() {
if t.root == nil {
return
}
qu := list.New()
topview := make(map[int]*tree)
qu.PushBack(top{t.root, 0})
topview[qu.Front().Value.(top).hd] = qu.Front().Value.(top).node
//fmt.Println(sample.Value.(top).hd)
fmt.Println("top view")
for qu.Len() > 0 {
sample := qu.Front()
qu.Remove(qu.Front())
for key := range topview {
if key != sample.Value.(top).hd {
topview[sample.Value.(top).hd] = sample.Value.(top).node
}
}
if sample.Value.(top).node.left != nil {
qu.PushBack(top{sample.Value.(top).node.left, sample.Value.(top).hd - 1})
}
if sample.Value.(top).node.right != nil {
qu.PushBack(top{sample.Value.(top).node.right, sample.Value.(top).hd + 1})
}
}
for _, value := range topview {
fmt.Println(value.data)
}
}
- 2 回答
- 0 关注
- 145 浏览
添加回答
举报