[Offer] [26] [Sub] Tree

Title Description

Two binary inputs A and B, B is not determined substructure A's. In FIG right sub tree structure on the left

Ideas analysis

  1. A tree is traversed to find the B tree root value of the same node R & lt;
  2. A decision tree in the R-rooted subtree contains the same B-tree structure.

Test Case

  1. Functional test: Tree A and B are ordinary binary tree; B is a tree structure or a sub-tree A is not.
  2. Special input test: two binary tree - a pointer or two nullptr root node; all nodes of the binary tree or subtree are no left and right subtrees.

Java code

public class Offer26 {
    public static void main(String[] args) {
        test1();
        test2();
        test3();
    }

    public static boolean hasSubtree(TreeNode root1, TreeNode root2) {
        return Solution1(root1, root2);
    }

    /**
     * 在树A中找到与树B中根节点值一样节点
     * 
     * @param root1
     * @param root2
     * @return
     */
    private static boolean Solution1(TreeNode root1, TreeNode root2) {
        boolean result = false;
        if (root1 != null && root2 != null) {
            if (isEqusl(root1.val, root2.val)) {
                result = DoesTreeHaveTree2(root1, root2);
            }
            if (!result) {
                result = Solution1(root1.left, root2);
            }
            if (!result) {
                result = Solution1(root1.right, root2);
            }
        }
        return result;
    }

    /**
     * 此方法用于判断树A中**以R为根结点**的子树是否包含B树一样的结构。
     * 
     * @param root1
     * @param root2
     * @return
     */
    private static boolean DoesTreeHaveTree2(TreeNode root1, TreeNode root2) {
        if (root2 == null) {
            return true;
        }
        if (root1 == null) {
            return false;
        }
        if (!isEqusl(root1.val, root2.val)) {
            return false;
        }
        return DoesTreeHaveTree2(root1.left, root2.left) && DoesTreeHaveTree2(root1.right, root2.right);
    }

    /**
     * 浮点数比较大小
     * 
     * @param num1
     * @param num2
     * @return
     */
    private static boolean isEqusl(float num1, float num2) {
        final float THRESHOLD = 0.000001f;
        if (Math.abs(num1 - num2) < THRESHOLD) {
            return true;
        } else {
            return false;
        }
    }

    private static void test1() {

    }

    private static void test2() {

    }

    private static void test3() {
    }

}

Code link

Offer to prove safety codes -Java

Guess you like

Origin www.cnblogs.com/haoworld/p/offer26-shu-de-zi-jie-gou.html