Print Nodes at Distance K from a Given Node

Given an arbitrary binary tree, a node of the tree, and an integer 'K', find all nodes that are at a distance K from the specified node, and return a list of these nodes.

The distance between two nodes in a binary tree is defined as the number of connections/edges in the path between the two nodes.

Input:

The first line of the input contains an integer T, denoting the number of test cases to run.

For each test case:
- The first line contains the values of the nodes of the tree in level order format (-1 representing a NULL node).
- The second line contains the value of the target node.
- The third line contains the integer K, representing the distance at which nodes are to be found.

Output:

For each test case, output the values of all nodes at distance = K from the specified target node.

Example:

The input example for the tree could be:

3
5 1
6 2 0 8
-1 -1 7 4 -1 -1 -1 -1
-1 -1 -1 -1

target: 5
K: 1

Output:
1 6 2

Constraints:

  • 1 <= T <= 100
  • 1 <= N <= 3000
  • 0 <= K <= 3000
  • 0 <= nodeValue <= 3000
  • Time Limit: 1 sec

Note:

1. A binary tree is a tree in which each node has at most two children.
2. The given tree will be non-empty.
3. The given tree can have multiple nodes with the same value.
4. If no nodes are at distance K from the given node, return an empty list.
5. You can return the list of values of valid nodes in any order.
AnswerBot
4mo

Given a binary tree, a target node, and an integer K, find all nodes at distance K from the target node.

  • Traverse the binary tree to find the target node.

  • Use BFS to traverse the tree from the target no...read more

Help your peers!
Select
Add answer anonymously...

Tower Research Capital LLC Software Developer interview questions & answers

A Software Developer was asked 9mo agoQ. Design an application similar to MakeMyTrip.
A Software Developer was asked Q. Check If Two Nodes Are Cousins You are given an arbitrary binary tree consisting...read more
A Software Developer was asked Q. Counting Nodes in a Complete Binary Tree - Problem Statement Given the root of a...read more

Popular interview questions of Software Developer

A Software Developer was asked 9mo agoQ1. Design an application similar to MakeMyTrip.
A Software Developer was asked Q2. Check If Two Nodes Are Cousins You are given an arbitrary binary tree consisting...read more
A Software Developer was asked Q3. Counting Nodes in a Complete Binary Tree - Problem Statement Given the root of a...read more
Tower Research Capital LLC Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits