package com.fishercoder.solutions;

import com.fishercoder.common.classes.TreeNode;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Set;

/**
 * 993. Cousins in Binary Tree
 *
 * 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:
 *        1
 *       / \
 *      2   3
 *     /
 *    4
 *
 * Input: root = [1,2,3,4], x = 4, y = 3
 * Output: false
 *
 * Example 2:
 *         1
 *        / \
 *       2   3
 *       \    \
 *       4    5
 *
 * Input: root = [1,2,3,null,4,null,5], x = 5, y = 4
 * Output: true
 *
 * Example 3:
 *          1
 *         / \
 *        2  3
 *        \
 *        4
 *
 * Input: root = [1,2,3,null,4], x = 2, y = 3
 * Output: false
 *
 *
 * Note:
 *
 * The number of nodes in the tree will be between 2 and 100.
 * Each node has a unique integer value from 1 to 100.
 */
public class _993 {
  public static class Solution1 {
    public boolean isCousins(TreeNode root, int x, int y) {
      Queue<TreeNode> queue = new LinkedList<>();
      queue.offer(root);
      while (!queue.isEmpty()) {
        int size = queue.size();
        for (int i = 0; i < size; i++) {
          TreeNode current = queue.poll();
          if (current.left != null) {
            queue.offer(current.left);
          }
          if (current.right != null) {
            queue.offer(current.right);
          }
          if (current.left != null && current.right != null) {
            if (current.left.val == x && current.right.val == y
                || current.left.val == y && current.right.val == x) {
              return false;
            }
          }
        }
        if (checkQueue(queue, x, y)) {
          return true;
        }
      }
      return false;
    }

    private boolean checkQueue(Queue<TreeNode> queue, int x, int y) {
      Set<Integer> set = new HashSet<>();
      Queue<TreeNode> tmp = new LinkedList<>(queue);
      while (!tmp.isEmpty()) {
        set.add(tmp.poll().val);
      }
      return set.contains(x) && set.contains(y);
    }
  }
}

@@ -77,16 +77,17 @@
 eeNode;%0A
+%0A
 import j
@@ -184,1044 +184,8 @@
 t;%0A%0A
-/**%0A * 993. Cousins in Binary Tree%0A *%0A * In a binary tree, the root node is at depth 0, and children of each depth k node are at depth k+1.%0A * Two nodes of a binary tree are cousins if they have the same depth, but have different parents.%0A * 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.%0A * Return true if and only if the nodes corresponding to the values x and y are cousins.%0A *%0A * Example 1:%0A *        1%0A *       / %5C%0A *      2   3%0A *     /%0A *    4%0A *%0A * Input: root = %5B1,2,3,4%5D, x = 4, y = 3%0A * Output: false%0A *%0A * Example 2:%0A *         1%0A *        / %5C%0A *       2   3%0A *       %5C    %5C%0A *       4    5%0A *%0A * Input: root = %5B1,2,3,null,4,null,5%5D, x = 5, y = 4%0A * Output: true%0A *%0A * Example 3:%0A *          1%0A *         / %5C%0A *        2  3%0A *        %5C%0A *        4%0A *%0A * Input: root = %5B1,2,3,null,4%5D, x = 2, y = 3%0A * Output: false%0A *%0A *%0A * Note:%0A *%0A * The number of nodes in the tree will be between 2 and 100.%0A * Each node has a unique integer value from 1 to 100.%0A */%0A
 publ
@@ -200,16 +200,18 @@
  _993 %7B%0A
+  
   public
@@ -236,16 +236,20 @@
 tion1 %7B%0A
+    
     publ
@@ -302,24 +302,30 @@
  y) %7B%0A      
+   
+   
 Queue%3CTreeNo
@@ -352,24 +352,30 @@
 edList%3C%3E();%0A
+      
       queue.
@@ -385,32 +385,38 @@
 er(root);%0A      
+ 
+     
 while (!queue.is
@@ -434,16 +434,24 @@
         
+     
+   
 int size
@@ -475,16 +475,24 @@
         
+       
+ 
 for (int
@@ -512,24 +512,34 @@
 ize; i++) %7B%0A
+          
           Tr
@@ -565,24 +565,34 @@
 eue.poll();%0A
+          
           if
@@ -609,32 +609,44 @@
 left != null) %7B%0A
+            
             queu
@@ -670,34 +670,54 @@
 eft);%0A          
+        
-%7D%0A
+  %7D%0A          
           if (cu
@@ -743,32 +743,44 @@
 ) %7B%0A            
+         
+   
 queue.offer(curr
@@ -793,34 +793,51 @@
 ght);%0A          
-%7D%0A
+          %7D%0A       
           if (cu
@@ -822,32 +822,35 @@
                 
+   
 if (current.left
@@ -890,32 +890,44 @@
 ) %7B%0A            
+        
+    
 if (current.left
@@ -978,16 +978,32 @@
         
+      
+          
 %7C%7C curre
@@ -1058,24 +1058,38 @@
             
+           
+   
 return false
@@ -1106,32 +1106,70 @@
     
-%7D%0A          %7D%0A        %7D%0A
+            %7D%0A                    %7D%0A                %7D%0A        
     
@@ -1199,24 +1199,34 @@
 e, x, y)) %7B%0A
+          
           re
@@ -1244,24 +1244,46 @@
         
+ 
+       %7D%0A            
 %7D%0A      
 %7D%0A      
@@ -1270,26 +1270,24 @@
     %7D%0A      
-%7D%0A
       return
@@ -1302,11 +1302,19 @@
     
-%7D%0A%0A
+    %7D%0A%0A    
     
@@ -1385,16 +1385,22 @@
 %7B%0A      
+   
+   
 Set%3CInte
@@ -1423,24 +1423,30 @@
 ashSet%3C%3E();%0A
+      
       Queue%3C
@@ -1488,24 +1488,30 @@
 eue);%0A      
+  
+    
 while (!tmp.
@@ -1531,16 +1531,24 @@
         
+      
+  
 set.add(
@@ -1570,24 +1570,36 @@
 ;%0A      
-%7D%0A
+      %7D%0A  
       
+    
 return s
@@ -1637,16 +1637,22 @@
 y);%0A    
-%7D%0A
+    %7D%0A  
   %7D%0A%7D%0A

refactor 993