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
    }
}

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) {
        if (s == null) {
            return false;
        }
        if (s.val == t.val) {
            boolean same = isSame(s, t);
            if (same) {
                return true;
            }
        }
        boolean a = isSubtree(s.left, t);
        boolean b = isSubtree(s.right, t);
        return a | b;
    }

    private static boolean isSame(TreeNode s, TreeNode t) {
        if (s == null && t != null) {
            return false;
        }
        if (s != null && t == null) {
            return false;
        }
        if (s == null && t == null) {
            return true;
        }
        if (s.val != t.val) {
            return false;
        }
        boolean left = isSame(s.left, t.left);
        boolean right = isSame(s.right, t.right);
        return left & right;
    }
}

Solve problem 572