DSA Tracker

Medium

All Nodes at Distance K in Binary Tree

A medium Binary Trees problem included in Striver A2Z. Below: the roles whose interviews prioritise this topic, and how to practise it.

Topic
Binary Trees
Sheets
1
Core for
4 roles
Platform
LeetCode

The problem

Given the root of a binary tree and an integer k, return all nodes at distance k from a given target node. The distance between two nodes is the number of edges on the path between them.

Example 1

Input
root=[3,5,1,6,2,0,8,null,null,7,4], target=5, k=2
Output
[7,4,1]
Why
Nodes at distance 2 from node 5 are 7 and 4 (children of node 2) and 1 (parent's sibling).

Example 2

Input
root=[1], target=1, k=3
Output
[]
Why
There are no nodes at distance 3 from the only node.

Example 3

Input
root=[0,1,null,2,3,null,4,null,5], target=0, k=2
Output
[2,3]
Why
Nodes at distance 2 from root 0 are nodes 2 and 3.

Constraints

  • The number of nodes is in the range [1, 500].
  • 0 <= Node.val <= 500
  • All values are unique.
  • 0 <= k <= 1000

How to think about it

Updated 2026-09-09

Distance in a tree radiates in three directions from any node: left child, right child, and parent. Standard binary tree nodes have no parent pointers, which traps traversals downwards. Build a parent lookup map first, turning the binary tree into an undirected graph, then run BFS outward from the target node.

Approaches, worst first

  1. DFS with ancestor distance tracking

    time O(n) · space O(h)

    Recursively search for target. When found, return distance to parent. Ancestors use that distance to search their opposite subtree for nodes at depth `k - dist`. Complex recursion with subtle boundary conditions.

  2. Parent pointer map with radial BFSWrite this one

    time O(n) · space O(n)

    Run a first BFS/DFS to populate a `parent` map for every node. Run a second BFS starting at `target`, expanding to left child, right child, and parent while tracking a `visited` set. Stop after exactly `k` radial expansion rounds.

Where people lose marks · 3
  • Omitting a `visited` set during radial BFS causes ping-pong cycles between parent and child nodes.
  • Handling `k = 0`: when distance is 0, the only valid answer is `[target.val]`, which BFS loops must handle without prematurely stepping to neighbours.
  • Comparing target by value when target is passed as a TreeNode reference, or failing to map the root's parent as null.

The theory behind it

Binary Trees — the ground this problem stands on. All Binary Trees problems

What Binary Trees is

A binary tree is a branching data structure that starts at a single top node called the root, like an upside-down family tree. Every node holds a piece of data and can branch out to at most two children below it, known as the left child and the right child. Because there is no ordering rule about which values go left or right, finding a specific item can require checking every single node in the entire tree.

When to reach for it

Reach for binary trees when problems present hierarchical data with left and right child pointers. Questions asking for tree height, maximum depth, path sums from root to leaf, diameter, lowest common ancestor, or checking whether two trees are mirror reflections of each other all signal binary tree traversals. Any problem asking to inspect or reconstruct a tree layer by layer or path by path belongs here.

How the pattern works

Think recursively by focusing on what a single node must do. If the current node is null, return the base answer immediately. Otherwise, ask the left child for its result, ask the right child for its result, and combine both answers with the current node value before returning up to the parent. For horizontal scans, use a queue to read nodes layer by layer, measuring the queue length at the start of each layer to group nodes by depth.

What each operation costs

OperationTime
traverse all nodes using recursion or queueO(n)
search for an arbitrary value in an unordered treeO(n)
call stack memory on balanced treeO(log n)
call stack memory on skewed treeO(n)
What usually goes wrong with Binary Trees
  • Dereferencing left or right child pointers without checking if the current node is null, throwing null pointer errors on empty trees or leaf nodes.
  • Defining a leaf node incorrectly by stopping when either child is null instead of checking that both left and right children are simultaneously null.
  • Computing tree diameter by taking left height plus right height inside a recursive helper without updating a global maximum across every visited node.

Which roles need this problem

Binary Trees is a core topic for these 4 roles — if you're targeting one of them, this problem is early in your path, not optional.

Secondary for 5 more roles, including Full-Stack Developer, Android Developer, iOS Developer.

Track this in your role's order

Pick your target role and all 370 problems — including this one — resequence to what that interview actually asks. Free.

Start free

More Binary Trees problems

Problem set and role mapping as of .