Question
Given a binary tree, find the number of nodes it contains.
Consider the following input:
flowchart TD
2((2)) --> 7((7))
7((7)) --> 4((4))
7((7)) --> 6((6))
2((2)) --> 9((9))
The expected output is 5
.
1
2
3
4
5
6
7
8
9
10
11
12
class Node:
def __init__(self, value) -> None:
self.value = value
self.left = None
self.right = None
def size_of_tree(root: Node):
if root is None:
return 0
return 1 + size_of_tree(root.left) + size_of_tree(root.right)