Skip to content

Latest commit

 

History

History
25 lines (24 loc) · 501 Bytes

添加0222.完全二叉树的节点个数Go版本.md

File metadata and controls

25 lines (24 loc) · 501 Bytes
func countNodes(root *TreeNode) int {
    if root == nil {
        return 0
    }
    q := list.New()
    q.PushBack(root)
    res := 0
    for q.Len() > 0 {
        n := q.Len()
        for i := 0; i < n; i++ {
            node := q.Remove(q.Front()).(*TreeNode)
            if node.Left != nil {
                q.PushBack(node.Left)
            }
            if node.Right != nil {
                q.PushBack(node.Right)
            }
            res++
        }
    }
    return res 
}