LeetCode 572 另一棵树的子树
方法1:DFS + 暴力匹配
class Solution {
public boolean isSubtree(TreeNode root, TreeNode subRoot) {
if(root == null) return false;
return isSameTree(root, subRoot) || isSubtree(root.left, subRoot) || isSubtree(root.right, subRoot);
}
static boolean isSameTree(TreeNode s, TreeNode t) {
if(s == null && t == null) return true;
if(s == null || t == null || s.val != t.val) return false;
return isSameTree(s.left, t.left) && isSameTree(s.right, t.right);
}
}
标签:return,04,08,2024,isSameTree,subRoot,TreeNode,null,root
From: https://www.cnblogs.com/XuGui/p/18341540