Nodes at Distance K from Target Node in Binary Tree

Given a Binary Tree of distinct integers and two integers, target and K, return a list of values of all nodes located at a distance K from the target node.

Explanation:

The distance between two nodes u and v is defined as the number of edges in the shortest path from u to v.

Input:

The first line contains an integer 'T', representing the number of test cases. Each test case consists of two parts:
1. A single line of space-separated integers representing the Binary Tree in level order, where -1 represents a null node.
2. Two integers, target and K.

Output:

A list of space-separated integers representing the values of nodes that are at distance K from the target node. Each test case should be printed on a new line.

Example:

Input:
1
1 2 3 4 -1 5 6 -1 7 -1 -1 -1 -1 -1 -1
2 2
Output:
7 5

Constraints:

  • 1 ≤ T ≤ 100
  • 0 ≤ N ≤ 3000
  • 0 ≤ data ≤ 10^4
  • 0 ≤ target ≤ 10^4
  • 1 ≤ K ≤ 3000

Where N is the total number of nodes in the binary tree, and data is the value of the binary tree node.

Note:
You do not need to print anything; it has already been taken care of. Just implement the given function.
AnswerBot
4mo

Find nodes at distance K from target node in a Binary Tree.

  • Use BFS to traverse the tree and keep track of the distance of each node from the target node.

  • Maintain a set to keep track of visited nodes t...read more

Help your peers!
Select
Add answer anonymously...

Media.net Software Services (India) Software Developer interview questions & answers

A Software Developer was asked Q. Given a sequence of positive numbers, make it a non-decreasing sequence. Operati...read more
A Software Developer was asked Q. Interleaving Two Strings Problem Statement You are given three strings 'A', 'B',...read more
A Software Developer was asked Q. Boxes of Power Problem Statement Given a set of boxes represented by an array ga...read more

Popular interview questions of Software Developer

A Software Developer was asked Q1. Given a sequence of positive numbers, make it a non-decreasing sequence. Operati...read more
A Software Developer was asked Q2. Interleaving Two Strings Problem Statement You are given three strings 'A', 'B',...read more
A Software Developer was asked Q3. Boxes of Power Problem Statement Given a set of boxes represented by an array ga...read more
Media.net Software Services (India) 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