Grokking Graph Algorithms for Coding Interviews
Ask Author
Back to course home

0% completed

Vote For New Content
Solution: All Nodes Distance K in Binary Tree
On this page

Problem Statement

Given the root of a binary tree, a target node, and a distance k, find all the nodes in the tree that are exactly k edges away from the target node. You may return these nodes' values in any order.

Examples

Example 1

  • Input: root = [1, 2, 3, 4, 5, null, 6, 7, 8], target = 3, k = 1
  • Expected Output: [6, 1]
  • Justification: Nodes 1 and 6 are one edge away from node 3.

Example 2

  • Input: root = [1, 2, 3, null, 4, 5, 6], tarrget = 2, k = 2
  • Expected Output: [3]
  • Justification: Node `3

.....

.....

.....

Like the course? Get enrolled and start learning!

On this page