Advertisement

Cousins in Binary Tree - LeetCode 993 Solution

Cousins in Binary Tree - Complete Solution Guide

Cousins in Binary Tree is LeetCode problem 993, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

Given the root of a binary tree with unique values and the values of two different nodes of the tree x and y , return true if the nodes corresponding to the values x and y in the tree are cousins , or false otherwise. Two nodes of a binary tree are cousins if they have the same depth with different parents. Note that in a binary tree, the root node is at the depth 0 , and children of each depth k node are at the depth k + 1 . Example 1: Input: root = [1,2,3,4], x = 4, y = 3 Output: false Example

Detailed Explanation

The problem asks us to determine if two nodes, 'x' and 'y', in a binary tree are cousins. Two nodes are considered cousins if they are at the same depth in the tree but have different parents. The tree is guaranteed to have unique values for each node, and both 'x' and 'y' are guaranteed to exist in the tree. The task is to return 'true' if 'x' and 'y' are cousins, and 'false' otherwise.

Solution Approach

The provided solutions utilize either DFS or BFS to traverse the binary tree, searching for the nodes with values 'x' and 'y'. During the traversal, the algorithm keeps track of the depth and parent of each node visited. Once 'x' and 'y' are found, their depths and parents are compared. If the depths are equal and the parents are different, the nodes are cousins, and the function returns 'true'. Otherwise, it returns 'false'.

Step-by-Step Algorithm

  1. Step 1: Initialize variables to store the parent and depth of nodes x and y. The initial values are set to default values to indicate that the nodes haven't been found yet.
  2. Step 2: Traverse the tree using either DFS or BFS. For DFS, a recursive function is used. For BFS, a queue is used.
  3. Step 3: During traversal, for each node, check if its value matches x or y. If a match is found, update the parent and depth accordingly.
  4. Step 4: For BFS approach, after processing each level, check if both x and y have been found. If so, break from the loop to improve efficiency.
  5. Step 5: After the traversal is complete (or after breaking out of the loop), compare the depths and parents of x and y.
  6. Step 6: If the depths are equal and the parents are different, return true. Otherwise, return false.

Key Insights

  • Insight 1: The core definition of cousins involves two conditions: same depth and different parents. Both must be checked.
  • Insight 2: Either Depth-First Search (DFS) or Breadth-First Search (BFS) can be used to traverse the tree and find the depth and parent of the target nodes.
  • Insight 3: The algorithm can terminate early once both nodes x and y are found, improving efficiency.

Complexity Analysis

Time Complexity: O(N)

Space Complexity: O(H)

Topics

This problem involves: Tree, Depth-First Search, Breadth-First Search, Binary Tree.

Companies

Asked at: Tekion.