您的位置:首页 > 编程语言 > Java开发

Check if tree b is part of tree a JAVA 实现

2016-09-16 02:01 295 查看
//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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐