与T100很像
class Solution { public boolean isSubtree(TreeNode root, TreeNode subRoot) { if(root == null) return false; if(dfs(root, subRoot)) return true; return isSubtree(root.left, subRoot) || isSubtree(root.right, subRoot); } public boolean dfs(TreeNode node1, TreeNode node2){ if(node1 == null && node2 == null) return true; if(node1 == null || node2 == null) return false; if(node1.val != node2.val) return false; return dfs(node1.left, node2.left) && dfs(node1.right, node2.right); } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)