package leetcode;

/**
 * https://leetcode.com/problems/subtree-of-another-tree/
 */
public class Problem572 {
    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }

    public boolean isSubtree(TreeNode s, TreeNode t) {
        // TODO
        return false;
    }

    public static void main(String[] args) {
        Problem572 prob = new Problem572();

        TreeNode s = new TreeNode(3);
        s.left = new TreeNode(4);
        s.left.left = new TreeNode(1);
        s.left.right = new TreeNode(2);
        s.right = new TreeNode(5);

        TreeNode t = new TreeNode(4);
        t.left = new TreeNode(1);
        t.right = new TreeNode(2);

        System.out.println(prob.isSubtree(s, t)); // true

        s = new TreeNode(3);
        s.left = new TreeNode(4);
        s.left.left = new TreeNode(1);
        s.left.right = new TreeNode(2);
        s.left.right.left = new TreeNode(0);
        s.right = new TreeNode(5);

        t = new TreeNode(4);
        t.left = new TreeNode(1);
        t.right = new TreeNode(2);

        System.out.println(prob.isSubtree(s, t)); // false
    }
}

@@ -335,16 +335,29 @@
     
-// TODO%0A
+if (s == null) %7B%0A    
     
@@ -382,109 +382,93 @@
     
+  
+  
 %7D
-%0A
 %0A    
-public static void main(String%5B%5D args) %7B%0A        Problem572 prob = new Problem572(
+    if (s.val == t.val) %7B%0A            boolean same = isSame(s, t
 );%0A
-%0A
         
 Tree
@@ -467,195 +467,225 @@
     
-TreeNode s = new TreeNode(3);%0A        s.left = new TreeNode(4);%0A        s.left.left = new TreeNode(1);%0A        s.left.right = new TreeNode(2);%0A        s.right = new TreeNode(5);%0A%0A        
+    if (same) %7B%0A                return true;%0A            %7D%0A        %7D%0A        boolean a = isSubtree(s.left, t);%0A        boolean b = isSubtree(s.right, t);%0A        return a %7C b;%0A    %7D%0A%0A    private static boolean isSame(
 Tree
@@ -689,23 +689,18 @@
 reeNode 
-t = new
+s,
  TreeNod
@@ -692,36 +692,37 @@
 Node s, TreeNode
-(4);
+ t) %7B
 %0A        t.left 
@@ -718,459 +718,392 @@
     
-t.lef
+if (s == null && 
 t 
+!
 = n
-ew TreeNode(1);%0A        t.right = new TreeNode(2);%0A%0A        System.out.println(prob.isSubtree(s, t)); // true%0A%0A        s = new TreeNode(3);%0A        s.left = new TreeNode(4);%0A        s.left.lef
+ull) %7B%0A            return false;%0A        %7D%0A        if (s != null && t == null) %7B%0A            return false;%0A        %7D%0A        if (s == null && 
 t =
+=
  n
-ew TreeNode(1);%0A        s.left.right = new TreeNode(2);%0A        s.left.right.left = new TreeNode(0);%0A        s.right = new TreeNode(5);%0A%0A        t = new TreeNode(4);%0A        t.left = new TreeNode(1);%0A        t.right = new TreeNode(2
+ull) %7B%0A            return true;%0A        %7D%0A        if (s.val != t.val) %7B%0A            return false;%0A        %7D%0A        boolean left = isSame(s.left, t.left);%0A        boolean right = isSame(s.right, t.right
 );%0A
-%0A
         
 Syst
@@ -1102,58 +1102,28 @@
     
-System.out.println(prob.isSubtree(s, t)); // false
+return left & right;
 %0A   

Solve problem 572