Finding nodes at distance K from a given node

In this article we will discuss the approach to find out the nodes at k distance from the given node.

Let us assume we have a binary tree. Let the node given to us be 'Target Node'. Now, we need to find all nodes at distance k from 'Target Node'. Those k nodes could be parent as well as child nodes of the 'Target Node'.

Below is the code that represents the implementation of the Tree:

class Tree{ int value; Tree right; Tree left;}

tree2-1

We can follow two major techniques:

1. Breadth Fir...

 •  0 comments  •  flag
Share on Twitter
Published on February 28, 2021 12:57
No comments have been added yet.