VOOZH about

URL: https://www.geeksforgeeks.org/dsa/print-nodes-at-k-distance-from-root/

⇱ Nodes at k distance from Root - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Nodes at k distance from Root

Last Updated : 2 May, 2026

Given a binary tree, and an integer k. The task is to return all the nodes which are at kdistance from the root. 

Examples:

Input:

πŸ‘ Print-nodes-at-k-distance-from-root

Output: 2 9 13
Explanation: In the above tree 2, 9 & 13 are at distance 2 from root. 

Input:

πŸ‘ Print-nodes-at-k-distance-from-root-2

Output: 5 11
Explanation: In the above tree 5 & 11 are at distance 1 from root.

[Naive Approach] Using Recursive Traversal (DFS) - O(n) time and O(h) Space

The idea is to use a recursive approach to find all nodes at a specified distance k from the root of a binary tree. or nodes at greater distances, the function recursively explores both left and right children, decrementing k with each call. And add the current node's data when k becomes 0.

Algorithm:

  • If root is NULL or k < 0, return
  • If k == 0, add current node’s value to result
  • Recur for left subtree with k - 1
  • Recur for right subtree with k - 1

Output
Nodes at distance 2: 2 9 13 

Time Complexity: O(n) where n is number of nodes in the given binary tree.
Space Complexity : O(h) where h is the height of binary tree.

[Better Approach] Using Queue - O(n) time and O(n) Space

This idea is based on line by line level order traversal. Start with an empty queue, enqueue the root, and set the level to 0. While the queue isn't empty, if the level equals k, store and return node values. For each node, dequeue it and enqueue its children. Increment the level after processing all nodes. return an empty list if no nodes are at distance k.

Algorithm:

  • Push root into the queue and set level or lvl = 0, then start level order traversal.
  • At each step, get current level size and check if lvl == k; if yes, store all node values and return.
  • Otherwise, remove each node and push its left and right children into the queue.
  • After processing the level, increment lvl and continue.
  • If the queue becomes empty before reaching k, return the empty result.

Output
Nodes at distance 2: 2 9 13 

Time Complexity: O(n) where n is number of nodes in the given binary tree.
Space Complexity: O(n)

[Expected Approach] Using Stack - O(n) time and O(n) Space

The idea is based on iterative (Stack based) Preorder traversal. We use a stack of pairs where we push level along with the node.

Algorithm:

  • Push root with level 0 into stack and start traversal.
  • Pop top element, if node is NULL, continue.
  • If its level equals k, add node value to result.
  • Otherwise, push the right child with level + 1, then the left child with level + 1.
  • Repeat until the stack becomes empty and return the result.

Output
Nodes at distance 2: 2 9 13 

Time Complexity: O(n) where n is the number of nodes in tree.
Auxiliary Space: O(n)

Comment
Article Tags: