Update 二叉树相关.md

master
Omooo 4 years ago
parent 9faefb8f2e
commit d37a3dab37
  1. 24
      blogs/Algorithm/剑指 Offer/二叉树相关.md

@ -65,3 +65,27 @@ class Solution {
} }
``` ```
#### [26. 树的子结构](https://leetcode-cn.com/problems/shu-de-zi-jie-gou-lcof/)
```java
class Solution {
public boolean isSubStructure(TreeNode A, TreeNode B) {
if (A == null || B == null) {
return false;
}
return isEquals(A, B) || isSubStructure(A.left, B) || isSubStructure(A.right, B);
}
private boolean isEquals(TreeNode nodeA, TreeNode nodeB) {
if (nodeB == null) {
return true;
}
if (nodeA == null || nodeA.val != nodeB.val) {
return false;
}
return isEquals(nodeA.left, nodeB.left) && isEquals(nodeA.right, nodeB.right);
}
}
```

Loading…
Cancel
Save