2Brute2Best
🔥0

All Nodes Distance K in Binary Tree

Medium·LeetCode #863 Pro OnlyMedium
LeetCode
01

Problem Statement

You are given the root of a binary tree, a node target that is in the tree, and an integer k. Return the values of all nodes that are exactly k edges away from target.

You can walk along any edge in either direction. So a node can be reached by going down from the target, or by going up to a parent first. The answer can be in any order.

Input

The root of a binary tree, the target node (a node that is in the tree), and an integer k.

Output

A list with the values of every node exactly k edges from target. Any order.

Example 1
Input: root = [3, 5, 1, 6, 2, 0, 8, null, null, 7, 4], target = 5, k = 2
Output: [7, 4, 1]

Nodes 7 and 4 are two edges below 5 (5 → 2 → 7 and 5 → 2 → 4). Node 1 is two edges away going up: 5 → 3 → 1.

Example 2
Input: root = [1], target = 1, k = 3
Output: []

The tree has only one node, so nothing is 3 edges away.

Constraints
  • • The number of nodes is in the range [1, 500]
  • • 0 ≤ Node.val ≤ 500
  • • All node values are unique
  • • target is a node in the tree
  • • 0 ≤ k ≤ 1000
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“all nodes exactly k away”
→ Not a path from the root. It is a ring of nodes around one node, and BFS grows the ring by one edge per round.
“from the target node”
→ The start is in the middle of the tree, not at the root, so the search must go up as well as down.
“values are unique”
→ Each node has one identity, so a visited set and a parent map can use nodes as keys.
03

Approach Complexity Ladder

Brute Force — distance from each node to the targetO(N²)O(H)

For every node, count the edges to the target through their split point; keep the ones equal to k

Optimal — parent map, then BFS from the targetO(N)O(N)

DFS records parents; BFS from the target over left, right and parent with a visited set; stop after k rounds

Brute Force Intuition

Take each node in turn and measure its distance to the target. The distance is the number of edges on the one path between them. That path goes up from each node to where their root paths split (their lowest common ancestor), then the two parts are added. If that distance equals k, the node is in the answer.

Recognition rule for this sub-pattern:

BFS reaches nodes in order of distance, so the first time you hit the target is the shortest. Add parent links to go upward.
Remember this · Tree BFS
Trigger
Level order, per-level max/average, right side view, minimum depth, nodes at distance k.
Think
Does the question care about depth / rows rather than paths?
Key question
How many nodes are in the current level (queue size at the start of the level)?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Tree BFS translate into production architectures and low-level memory mechanics.