package com.fishercoder.solutions;

import com.fishercoder.common.classes.TreeNode;
import java.util.ArrayList;
import java.util.List;

/**
 * 872. Leaf-Similar Trees
 *
 * Consider all the leaves of a binary tree.  From left to right order, the values of those leaves form a leaf value sequence.
 *
 *          3
 *         / \
 *        5   1
 *       / \ / \
 *      6  2 9 8
 *        / \
 *       7  4
 *
 * For example, in the given tree above, the leaf value sequence is (6, 7, 4, 9, 8).
 *
 * Two binary trees are considered leaf-similar if their leaf value sequence is the same.
 *
 * Return true if and only if the two given trees with head nodes root1 and root2 are leaf-similar.
 *
 * Note:
 *
 * Both of the given trees will have between 1 and 100 nodes.
 */
public class _872 {
  public static class Solution1 {
    public boolean leafSimilar(TreeNode root1, TreeNode root2) {
      List<Integer> leaves1 = new ArrayList<>();
      List<Integer> leaves2 = new ArrayList<>();
      preorder(root1, leaves1);
      preorder(root2, leaves2);
      return leaves1.equals(leaves2);
    }

    private void preorder(TreeNode root,
        List<Integer> leaves) {
      if (root == null) {
        return;
      }
      if (root.left == null && root.right == null) {
        leaves.add(root.val);
      }
      preorder(root.left, leaves);
      preorder(root.right, leaves);
    }
  }
}

@@ -77,16 +77,17 @@
 eeNode;%0A
+%0A
 import j
@@ -134,644 +134,8 @@
 t;%0A%0A
-/**%0A * 872. Leaf-Similar Trees%0A *%0A * Consider all the leaves of a binary tree.  From left to right order, the values of those leaves form a leaf value sequence.%0A *%0A *          3%0A *         / %5C%0A *        5   1%0A *       / %5C / %5C%0A *      6  2 9 8%0A *        / %5C%0A *       7  4%0A *%0A * For example, in the given tree above, the leaf value sequence is (6, 7, 4, 9, 8).%0A *%0A * Two binary trees are considered leaf-similar if their leaf value sequence is the same.%0A *%0A * Return true if and only if the two given trees with head nodes root1 and root2 are leaf-similar.%0A *%0A * Note:%0A *%0A * Both of the given trees will have between 1 and 100 nodes.%0A */%0A
 publ
@@ -150,16 +150,18 @@
  _872 %7B%0A
+  
   public
@@ -186,16 +186,20 @@
 tion1 %7B%0A
+    
     publ
@@ -251,24 +251,30 @@
 de root2) %7B%0A
+      
       List%3CI
@@ -306,24 +306,30 @@
 ayList%3C%3E();%0A
+      
       List%3CI
@@ -361,24 +361,30 @@
 ayList%3C%3E();%0A
+      
       preord
@@ -401,32 +401,38 @@
 leaves1);%0A      
+     
+ 
 preorder(root2, 
@@ -437,24 +437,30 @@
 , leaves2);%0A
+      
       return
@@ -481,24 +481,28 @@
 s(leaves2);%0A
+    
     %7D%0A%0A    p
@@ -500,16 +500,20 @@
  %7D%0A%0A    
+ 
+   
 private 
@@ -541,16 +541,38 @@
 e root,%0A
+                      
         
@@ -597,24 +597,30 @@
 es) %7B%0A      
+     
+ 
 if (root == 
@@ -631,24 +631,32 @@
 ) %7B%0A        
+     
+   
 return;%0A    
@@ -657,24 +657,36 @@
 ;%0A      
-%7D%0A
+      %7D%0A   
       
+   
 if (root
@@ -716,32 +716,40 @@
 ight == null) %7B%0A
+        
         leaves.a
@@ -764,26 +764,38 @@
 val);%0A      
+ 
-%7D%0A
+     %7D%0A      
       preord
@@ -819,24 +819,30 @@
 ves);%0A      
+ 
+     
 preorder(roo
@@ -863,16 +863,22 @@
 s);%0A    
-%7D%0A
+    %7D%0A  
   %7D%0A%7D%0A

refactor 872