LeetCode-in-Java

993. Cousins in Binary Tree

Easy

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 2:

Input: root = [1,2,3,null,4,null,5], x = 5, y = 4

Output: true

Example 3:

Input: root = [1,2,3,null,4], x = 2, y = 3

Output: false

Constraints:

Solution

import com_github_leetcode.TreeNode;

/*
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
public class Solution {
    public boolean isCousins(TreeNode root, int x, int y) {
        return !isSiblings(root, x, y) && isSameLevels(root, x, y);
    }

    private boolean isSameLevels(TreeNode root, int x, int y) {
        return findLevel(root, x, 0) == findLevel(root, y, 0);
    }

    private int findLevel(TreeNode root, int x, int level) {
        if (root == null) {
            return -1;
        }
        if (root.val == x) {
            return level;
        }
        int leftLevel = findLevel(root.left, x, level + 1);
        if (leftLevel == -1) {
            return findLevel(root.right, x, level + 1);
        } else {
            return leftLevel;
        }
    }

    private boolean isSiblings(TreeNode root, int x, int y) {
        if (root == null) {
            return false;
        }
        // Check children first
        boolean leftSubTreeContainsCousins = isSiblings(root.left, x, y);
        boolean rightSubTreeContainsCousins = isSiblings(root.right, x, y);
        if (leftSubTreeContainsCousins || rightSubTreeContainsCousins) {
            return true;
        }
        if (root.left == null || root.right == null) {
            return false;
        }
        // Check for siblings at parent
        return root.left.val == x && root.right.val == y
                || root.right.val == x && root.left.val == y;
    }
}