Dsa
Trees
Easy
Count Nodes at Depth K
Given a binary tree and a non-negative integer K, write a function that counts the number of nodes at depth K. The root node is at depth 0. If K is greater than the height of the tree, return 0.
Input format:
A series of integers in level order, where 'N' represents a null node, followed by an integer K.
Example Input:
1 2 3 N N 4 5 2
Output format:
An integer representing the count of nodes at depth K.
Example:
Input:
1 2 3 N N 4 5 2
Output:
2
Key concepts
treesdepthcounting
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now