Skip to content

Tree Size

01 · Question

Return the number of nodes in a binary tree.

02 · Solution

Reference solution

1def treeSize(root: Optional[TreeNode]) -> int:
2 if not root:
3 return 0
4 return 1 + treeSize(root.left) + treeSize(root.right)