# Question

Formatted question description: https://leetcode.ca/all/993.html

In a binary tree, the root node is at depth 0, and children of each depth k node are at depth k+1.

Two nodes of a binary tree are cousins if they have the same depth, but have different parents.

We are given the root of a binary tree with unique values, and the values x and y of two different nodes in the tree.

Return true if and only if the nodes corresponding to the values x and y are cousins.

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:

The number of nodes in the tree will be between 2 and 100.
Each node has a unique integer value from 1 to 100.


# Algorithm 1

This question defines a binary tree number of cousin nodes, that is, they do not belong to the same parent node, but have the same depth. Now two node values are given and asked whether the nodes they represent are cousin nodes. Since the cousin nodes must belong to the same level, the level order traversal of the binary tree can be used, just like the previous Binary Tree Level Order Traversal.

Two additional boolean variables isX and isY are needed here to record whether x and y have been traversed. Since it is a layer sequence traversal, a for loop is needed in the while. In the loop, take the first node of the line and see if the node value is equal to x or y. If yes, mark the Boolean variable. Then check whether the left and right child nodes of the current node are x and y respectively, if yes, return false directly. Otherwise, the left and right child nodes are queued, if they exist. After the current layer is traversed, check the values of isX and isY, if both are true, it means that there is a cousin node, and return true. If only one is true, it means that the two are not in the same layer and return false directly. See the code as follows:

C++

# Algorithm 2

Of course, we can also use the recursive method. Since we cannot process the nodes of the same layer at the same time, we need two variables depthX and depthY to record the depth of the nodes x and y, and use a Boolean variable sameParent to record these two Whether the two nodes have the same parent node. In the recursive function, if the current node node is empty, return directly. If the current node value is x, then depthX is assigned to the current depth cur. Similarly, if the current node value is y, then depthY is assigned to the current depth cur. Then check whether the left and right child nodes of the current node are x and y respectively. If yes, mark the sameParent as true, and finally call recursion on the left and right child nodes respectively, see the code as follows:

C++

# Algorithm 3

The following method is relatively simple. It is to establish a mapping between each node and the pair composed of its depth and the parent node, and then you can directly use x and y can get its depth and compare with the parent node, see the code as follows:

# Code 3

C++

Java

/**
* Definition for a binary tree node.
* public class TreeNode {
*     int val;
*     TreeNode left;
*     TreeNode right;
*     TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isCousins(TreeNode root, int x, int y) {
if (root == null || x == y)
return false;
TreeNode xParent = null, yParent = null;
int xDepth = -1, yDepth = -1;
nodeQueue.offer(root);
depthQueue.offer(0);
while (!nodeQueue.isEmpty() && (xDepth < 0 || yDepth < 0)) {
TreeNode node = nodeQueue.poll();
int depth = depthQueue.poll();
TreeNode left = node.left, right = node.right;
if (left != null) {
nodeQueue.offer(left);
depthQueue.offer(depth + 1);
if (left.val == x) {
xParent = node;
xDepth = depth + 1;
}
if (left.val == y) {
yParent = node;
yDepth = depth + 1;
}
}
if (right != null) {
nodeQueue.offer(right);
depthQueue.offer(depth + 1);
if (right.val == x) {
xParent = node;
xDepth = depth + 1;
}
if (right.val == y) {
yParent = node;
yDepth = depth + 1;
}
}
}
return xDepth == yDepth && xParent != yParent;
}
}