ORIGINAL CODING QUESTION
Mentoring tree leaf count
Count nodes with no children in the rooted tree, including the root when it is the only node.
Original exercise · easy. Editorial role relevance does not imply a company has asked this question.
Input and output
n, followed by n parent IDs. Node 0 is root with parent -1; every other parent is in [0, node ID).
Constraints
1 ≤ n ≤ 200; the parent list describes one connected rooted tree.
Example 1
Input
4 -1 0 1 1
Output
2
Nodes 2 and 3 are leaves.
Example 2
Input
1 -1
Output
1
The lone root is a leaf.
Explain your approach
Explain your time and auxiliary space complexity. Which input makes your algorithm work hardest?
Private grading inputs, expected outputs and canonical reference implementations are withheld. Authored expected outputs were cross-checked with independent reference oracles; compiler/runtime behavior still needs a verified execution provider.
Hint 1
A leaf appears as no other node’s parent.
Hint 2
Count children for each node.
Hint 3
A zero child count identifies a leaf.
Practice this question
Save your work privately, explain decisions and return to your revision queue.
Open practice workspaceBrowse the question library →