Files
LeetCode-Go/leetcode/0958.Check-Completeness-of-a-Binary-Tree/0958.Check Completeness of a Binary Tree.go
2021-09-15 03:51:40 -07:00

36 lines
732 B
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package leetcode
import (
"github.com/halfrost/LeetCode-Go/structures"
)
// TreeNode define
type TreeNode = structures.TreeNode
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func isCompleteTree(root *TreeNode) bool {
queue, found := []*TreeNode{root}, false
for len(queue) > 0 {
node := queue[0] //取出每一层的第一个节点
queue = queue[1:]
if node == nil {
found = true
} else {
if found {
return false // 层序遍历中,两个不为空的节点中出现一个 nil
}
//如果左孩子为nil则append进去的node.Left为nil
queue = append(queue, node.Left, node.Right)
}
}
return true
}