Geeksforgeeks

Error

×

Leaderboard

Showing:

Handle | Score |
---|---|

@Ibrahim Nash | 6379 |

@blackshadows | 6329 |

@mb1973 | 5358 |

@Quandray | 5231 |

@akhayrutdinov | 5111 |

@saiujwal13083 | 4510 |

@sanjay05 | 3762 |

@marius_valentin_dragoi | 3522 |

@sushant_a | 3459 |

@verma_ji | 3357 |

@KshamaGupta | 3318 |

Complete Leaderboard | |

Handle | Score |

@ritiksethi21 | 1050 |

@aroranayan999 | 807 |

@RizulBansal | 685 |

@ashishtrehan002 | 538 |

@hemantgarg923 | 528 |

@simrangoyal | 526 |

@ronaldo77 | 520 |

@anishrajan | 505 |

@thanosagain | 505 |

@ssparteek470 | 495 |

@rahul2312 | 491 |

Complete Leaderboard |

Given the binary Tree of and two-node values. Check whether the two-node values are cousins of each other or not.

**Example 1:**

**Input:
**1
/ \
** **2 3
a = 2, b = 3**
Output: **0**
**

**Example 2:**

**Input:
** 1
/ \
2 3
/ \
** **5 4**
**a = 5, b = 4**
Output: **1**
Explanation: **Here, nodes 5 and 4 are
at the same level and have different
parent nodes. Hence, they both are cousins

**Your Task:**

You don't need to read input or print anything. Your task is to complete the function** isCousins() **that takes the root node** **of the tree (having all nodes distinct)**, **and two integers 'a' and 'b' as inputs. It returns true if the nodes with given values 'a' and 'b' are Cousins of each other and returns false otherwise.

Two nodes value are cousins of each other if they are at the same level and have different parents. (The driver code will print 1 if the returned values is true,otherwise 0.)

**Expected Time Complexity: **O(N).

**Expected Auxiliary Space: **O(Height of the Tree).

**Constraints:**

1<=Number of Nodes<=1000

**Note: **The **Input/Ouput** format and **Example** given are used for system's internal purpose, and should be used by a user for **Expected Output** only. As it is a function problem, hence a user should not read any input from stdin/console. The task is to complete the function specified, and not to write the full code.

Login to report an issue on this page.

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

Yes
Check if two Nodes are Cousins

...