题目描述
输入两颗二叉树A,B,判断B是不是A的子结构。
public class Solution { public boolean HasSubtree(TreeNode T1, TreeNode T2) { if (T2 == null || T1==null) { return false; } if (isEqual(T1, T2)) { return true; } if (HasSubtree(T1.left, T2) || HasSubtree(T1.right, T2)) { return true; } return false; } private boolean isEqual(TreeNode T1, TreeNode T2) { if (T2 == null) { return true; } if(T1 == null){ return false; } if (T1.val != T2.val) { return false; } return isEqual(T1.left, T2.left) && isEqual(T1.right, T2.right); } }