另一棵树的子树
另一棵树的子树
题目描述
解法一 DFS
代码演示:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public boolean isSubtree(TreeNode s, TreeNode t) {
return dfs(s, t);
}
public boolean dfs(TreeNode s, TreeNode t){
if(s == null){
return false;
}
return check(s,t) || dfs(s.left,t) || dfs(s.right,t);
}
public boolean check(TreeNode s, TreeNode t){
if(s == null && t == null){
return true;
}
if(s == null || t == null || s.val != t.val){
return false;
}
return check(s.left,t.left) && check(s.right,t.right);
}
}
解法二 KMP 算法
代码演示:
public static boolean isSubtree(TreeNode big, TreeNode small) {
if (small == null) {
return true;
}
if (big == null) {
return false;
}
ArrayList<String> b = preSerial(big);
ArrayList<String> s = preSerial(small);
String[] str = new String[b.size()];
for (int i = 0; i < str.length; i++) {
str[i] = b.get(i);
}
String[] match = new String[s.size()];
for (int i = 0; i < match.length; i++) {
match[i] = s.get(i);
}
return getIndexOf(str, match) != -1;
}
/**
* 前序序列化
* @param head
* @return
*/
public static ArrayList<String> preSerial(TreeNode head) {
ArrayList<String> ans = new ArrayList<>();
pres(head, ans);
return ans;
}
/**
* 前序遍历
* @param head
* @param ans
*/
public static void pres(TreeNode head, ArrayList<String> ans) {
if (head == null) {
ans.add(null);
} else {
ans.add(String.valueOf(head.val));
pres(head.left, ans);
pres(head.right, ans);
}
}
/**
* KMP 算法
* @param str1
* @param str2
* @return
*/
public static int getIndexOf(String[] str1, String[] str2) {
if (str1 == null || str2 == null || str1.length < 1 || str1.length < str2.length) {
return -1;
}
int x = 0;
int y = 0;
int[] next = getNextArray(str2);
while (x < str1.length && y < str2.length) {
if (isEqual(str1[x], str2[y])) {
x++;
y++;
} else if (next[y] == -1) {
x++;
} else {
y = next[y];
}
}
return y == str2.length ? x - y : -1;
}
/**
* 前缀数组
* @param ms
* @return
*/
public static int[] getNextArray(String[] ms) {
if (ms.length == 1) {
return new int[] { -1 };
}
int[] next = new int[ms.length];
next[0] = -1;
next[1] = 0;
int i = 2;
int cn = 0;
while (i < next.length) {
if (isEqual(ms[i - 1], ms[cn])) {
next[i++] = ++cn;
} else if (cn > 0) {
cn = next[cn];
} else {
next[i++] = 0;
}
}
return next;
}
/**
* 判断是否相等
* @param a
* @param b
* @return
*/
public static boolean isEqual(String a, String b) {
if (a == null && b == null) {
return true;
} else {
if (a == null || b == null) {
return false;
} else {
return a.equals(b);
}
}
}
KMP 算法
KMP–高效字符串匹配算法
Manacher算法 – 回文长度算法