Find if two nodes are cousins in Binary Tree

ad1tyaV
Jul 1, 2021

--

We say two nodes in a binary tree are cousins if they are on same level but have different parents

Algorithm:

  1. Find the parent nodes and levels of the given nodes
  2. Store these in a pair to compare
  3. If they happen to have different parents and same level, then they are cousins, else they are not!

The code should be self explanatory.

The Binary Tree for the below program.

Thanks for Reading!!1

--

--

ad1tyaV
ad1tyaV

No responses yet