Grokking the Engineering Manager Coding Interview
Ask Author
Back to course home

0% completed

All Nodes Distance K in Binary Tree (medium)
Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

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!

Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible