【剑指Offer 26】树的子结构
作者:互联网
/**
* 剑指 Offer 26. 树的子结构
* https://leetcode.cn/problems/shu-de-zi-jie-gou-lcof/
* */
public class Solution {
public boolean isSubStructure(TreeNode A, TreeNode B) {
if (A == null || B == null) {
return false;
}
if (isEquals(A, B)) {
return true;
}
return isSubStructure(A.left, B) || isSubStructure(A.right, B);
}
public boolean isEquals(TreeNode a, TreeNode b) {
// 子树已遍历完
if (b == null) {
return true;
}
// 子树未完但主树已完
if (a == null) {
return false;
}
if (a.val != b.val) {
return false;
}
return isEquals(a.left, b.left) && isEquals(a.right, b.right);
}
}
标签:26,right,return,Offer,isEquals,子结构,TreeNode,false,null 来源: https://www.cnblogs.com/liaozibo/p/offer-26.html