You are given the root of a binary tree. Your task is to count the number of non-leaf (internal) nodes.
A non-leaf node is a node that has at least one child.
Return the total number of nodes that are not leaf nodes.
Input: root = [1,2,3,4,5]
Output: 2
Explanation: Nodes 1 and 2 have children; nodes 3, 4, and 5 are leaves. So there are 2 non-leaf nodes.
Input: root = [1]
Output: 0
Explanation: A single node is a leaf, so there are no non-leaf nodes.
Input: root = []
Output: 0
Explanation: An empty tree has no non-leaf nodes.
Approach, complexity and clean code in every language. Sign in to unlock the editorial.
Your submissions, results and runtime history live here. Sign in to start your streak.
1 2 3 4 5 6 7
3
10 5 15 2 7 12 20
3