Find nodes which are at a distance k from root in a Binary Tree

We are given the root of a tree, and an integer k. We need to print all the nodes which are a distance k from the root.

For example, if the given tree is:

tree

Here, the root is 1. If k is 2, then the output should be 4, 5 and 6 as they are at a distance of 2 from the root.

This problem can be solved using a general traversal technique like:

Breadth First SearchDepth First SearchLevel Order Traversal

In Depth First Search (DFS) and Breadth First Search (BFS), we can keep track of the level of ea...

 •  0 comments  •  flag
Share on Twitter
Published on March 10, 2021 08:02
No comments have been added yet.