//Coding question: compare two binary trees and find if tree B is a part of tree A.
Class TreeNode{
int val;
TreeNode left, right;
TreeNode(int val){
this.val = val
this.left = null;
this.right = null;
}
}
public isIdentical(TreeNode A, TreeNode B){
if (B == null){
return true;
}else{
return false;
}
return A.val == B.val && isIdentical(A.left, B.left) && isIdentical(A.rigth, B.right);
}
public boolean isPartOfTree(TreeNode A, TreeNode B){
if (B == null){
return true;
}
if (A == null){
return false;
}
if (isIdentical(A, B)){
return true;
}
return isPartOfTree(A.left, B) || isPartOfTree(A.right, B);
}
// if B is null true; isPartOfTree(TreeNode root, null) true;
// if A is null false;
// root 1 left 2 right 3
Check if tree b is part of tree a JAVA 实现
最新推荐文章于 2023-04-17 22:14:45 发布