/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean checkSubTree(TreeNode t1, TreeNode t2) {
// 条件过滤
if (t2 == null){
return true;
}
if (t1 == null){
return false;
}
// 判断是否想等
return bfs(t1, t2) || checkSubTree(t1.left, t2) || checkSubTree(t1.right, t2);
}
private boolean bfs(TreeNode t1, TreeNode t2){
if (t1 == null && t2 == null) return true;
if (t1 == null || t2 == null || t1.val != t2.val) return false;
return bfs(t1.left, t2.left) && bfs(t1.right, t2.right);
}
}
力扣:面试题 04.10. 检查子树
最新推荐文章于 2024-10-12 17:26:00 发布