VOOZH about

URL: https://www.geeksforgeeks.org/dsa/print-nodes-distance-k-leaf-node/

⇱ Print all nodes that are at distance k from a leaf node - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Print all nodes that are at distance k from a leaf node

Last Updated : 23 Jul, 2025

Given a Binary Tree and a positive integer K, print all nodes that are distance K from a leaf node. Here K distance from a leaf means K levels higher than a leaf node. For example, if K is more than the height of the Binary Tree, then nothing should be printed. 

Examples:

👁 distKfromLeaf

Recommended Practice

To solve the problem follow the below idea: 

We can store the nodes in the path of our recursion and whenever we reach a leaf node, then print the Kth node in the saved path

Follow the below steps to solve the problem:

  • Traverse the tree and keep storing all ancestors till we hit a leaf node. 
  • When we reach a leaf node, we print the ancestor at distance K using the values stored in the array. 
  • We also need to keep track of nodes that are already printed as output. For that, we use a boolean array visited[]

Below is the implementation of the above approach:


Output
Nodes at distance 2 are: 1 3 

Time Complexity: O(N)
Auxiliary Space: O(N)

Approach 2: Iterative Solution

In this approach, we use an iterative method to traverse the binary tree using a stack. We maintain a stack of pairs, where each pair consists of a node and its depth. We start by pushing the root node and its depth onto the stack. Then, we repeatedly pop a node from the stack and push its left and right children, along with their depths, onto the stack. When we encounter a leaf node, we check its depth and see if it is at a distance k from the current node. If it is, we print the current node.

  • Initialize an empty stack st, an empty vector path, and an empty vector visited.
  • Push a pair (root, 0) onto the stack, where root is the root of the binary tree and 0 is the depth of root.
  • While st is not empty, do the following:
    •  Pop the top pair (curr, depth) from st.
    • If curr is nullptr, continue to the next iteration of the loop.
    • Append curr->val to path and false to visited.
    • If curr is a leaf node, check if its depth is k less than the current depth of curr. If so, check if the node at that depth has already been visited. If not, print the value of the node at that depth and mark it as visited.
    • Push the pairs (curr->left, depth + 1) and (curr->right, depth + 1) onto st.
    • If depth + 1 is less than the size of path, resize path and visited to remove the nodes beyond depth + 1.

Here's the codes for this approach:


Output
1 3 

Time Complexity:
In the worst case, we visit every node in the binary tree once. For each node, we append its value to path and false to visited, which takes O(1) time. If the node is a leaf node, we check its depth and mark the corresponding node in visited if necessary, which also takes O(1) time. Finally, we push its left and right children onto st, which takes O(1) time each. Thus, the time complexity of the algorithm is O(n), where n is the number of nodes in the binary tree.

Space Complexity:
The space used by the algorithm is proportional to the maximum depth of the binary tree. In the worst case, the binary tree is a skewed tree, in which case the maximum depth is n. In this case, the size of path and visited is O(n), and the size of the stack st is also O(n). Thus, the space complexity of the algorithm is O(n). However, in the best case, the binary tree is a balanced tree, in which case the maximum depth is log(n). In this case, the space complexity is O(log(n)).

Comment
Article Tags:
Article Tags: