This post is completed by 2 users

  • 0
Add to List
Medium

62. In a Binary Tree, Check if Two nodes has the same parent or are siblings

Objective: In a Binary Tree, Check if Two nodes has the same parent or are siblings

Example:

Siblings Nodes

Approach:

  • Given, root, Node x, Node y.
  • Check if x and y are childs of root. (root.left==x && root.right==y) ||root.left==y && root.right==x)
  • if yes then return true.
  • Else make a recursive call to root.left and root.right
Node 2 and Node 3 are siblings??? true
Node 7 and Node 9 are siblings??? false