@@ -0,0 +1,1701 @@
+package com.freetymekiyan.algorithms.level.easy;%0A%0Aimport com.freetymekiyan.algorithms.utils.Utils.TreeNode;%0A%0A/**%0A * 572. Subtree of Another Tree%0A * Given two non-empty binary trees s and t, check whether tree t has exactly the same structure and node values with a%0A * subtree of s. A subtree of s is a tree consists of a node in s and all of this node's descendants. The tree s could%0A * also be considered as a subtree of itself.%0A * %3Cp%3E%0A * Example 1:%0A * Given tree s:%0A * %3Cp%3E%0A * %7C     3%0A * %7C    / %5C%0A * %7C   4   5%0A * %7C  / %5C%0A * %7C 1   2%0A * Given tree t:%0A * %7C   4%0A * %7C  / %5C%0A * %7C 1   2%0A * Return true, because t has the same structure and node values with a subtree of s.%0A * Example 2:%0A * Given tree s:%0A * %3Cp%3E%0A * %7C     3%0A * %7C    / %5C%0A * %7C   4   5%0A * %7C  / %5C%0A * %7C 1   2%0A * %7C    /%0A * %7C   0%0A * Given tree t:%0A * %7C   4%0A * %7C  / %5C%0A * %7C 1   2%0A * Return false.%0A * %3Cp%3E%0A * Related Topics: Tree%0A * Similar Questions: (M) Count Univalue Subtrees, (M) Most Frequent Subtree Sum%0A */%0Apublic class SubtreeOfAnotherTree %7B%0A%0A%0A    /**%0A     * Recursive.%0A     * If s has a subtree like t, t can be:%0A     * 1. A subtree starting at s.%0A     * 2. A subtree in s's left subtree.%0A     * 3. A subtree in s's right subtree.%0A     */%0A    public boolean isSubtree(TreeNode s, TreeNode t) %7B%0A        if (s == null && t == null) return true;%0A        if (s == null %7C%7C t == null) return false;%0A        return (s.val == t.val && dfs(s, t)) %7C%7C%0A                isSubtree(s.left, t) %7C%7C isSubtree(s.right, t);%0A    %7D%0A%0A    private boolean dfs(TreeNode s, TreeNode t) %7B%0A        if (s == null && t == null) return true;%0A        if (s == null %7C%7C t == null) return false;%0A        return s.val == t.val && dfs(s.left, t.left) && dfs(s.right, t.right);%0A    %7D%0A%7D%0A

add 572. Subtree of Another Tree