leetcode:222. count complete tree nodes

题目描述

Given a complete binary tree, count the number of nodes.

Note:

Definition of a complete binary tree from Wikipedia:
In a complete binary tree every level, except possibly the last, is completely filled, and all nodes in the last level are as far left as possible. It can have between 1 and 2h nodes inclusive at the last level h.

Example:

1
2
3
4
5
6
7
8
Input: 
1
/
2 3
/ /
4 5 6

Output: 6

代码实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution:
def countNodes(self, root: TreeNode) -> int:
result = 0
if root == None:
return 0

nodes = [root]
while len(nodes) > 0:
tmp = nodes.pop(0)
result += 1
if tmp.left != None:
nodes.append(tmp.left)
if tmp.right != None:
nodes.append(tmp.right)

return result