572. 另一棵树的子树
解题思路
- 遍历二叉树的思路
- 针对每一个节点判断该节点的子树和subtree是不是相等
- 需要编写判断两个子树是否相等的函数
class Solution {public boolean isSame(TreeNode root,TreeNode root1){if(root == null && root1 == null){return true;}if(root == null || root1 == null){return false;}if(root.val != root1.val){return false;}return isSame(root.left,root1.left) && isSame(root.right,root1.right);}public boolean isSubtree(TreeNode root, TreeNode subRoot) {if(root == null){return subRoot == null;}if(isSame(root,subRoot)){return true;}return isSubtree(root.left,subRoot) || isSubtree(root.right,subRoot);}
}