Tree Size

Return the number of nodes in a binary tree.

1def treeSize(root: Optional[TreeNode]) -> int:
2 if not root:
3 return 0
4 return 1 + treeSize(root.left) + treeSize(root.right)
node
1
0
2
1
3
2
tree=[1, 2, 3]
Step 1 / 3
Step 1:
At each node, count 1 plus sizes of left and right subtrees.
Pointers: node=0
Focus: select @ [0]