Skip to content

Commit 1a75c6a

Browse files
committed
feat: count-complete-tree-nodes
1 parent bc41bcb commit 1a75c6a

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

Diff for: bt.count-complete-tree-nodes.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Definition for a binary tree node.
2+
class TreeNode:
3+
def __init__(self, x):
4+
self.val = x
5+
self.left = None
6+
self.right = None
7+
8+
class Solution:
9+
"""
10+
222. 完全二叉树的节点个数
11+
https://leetcode-cn.com/problems/count-complete-tree-nodes
12+
说明:
13+
完全二叉树的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,
14+
并且最下面一层的节点都集中在该层最左边的若干位置。若最底层为第 h 层,则该层包含 1~ 2h 个节点。
15+
"""
16+
def countNodes(self, root: TreeNode) -> int:
17+
if not root:
18+
return 0
19+
20+
return 1 + self.countNodes(root.left) + self.countNodes(root.right)

0 commit comments

Comments
 (0)