我正在研究一个代码,用于确定2个BST是否在值上相等。检查2个BST是否等于数值
这就是我想出的,但它只是返回true,我真的不知道问题是什么。
public boolean same(BSTree t2) {
return sameTree(root, t2);
}
private boolean sameTree(TreeNode n, BSTree other) {//L-M-R
boolean found = false;
if (n != null) {
sameTree(n.getLeftNode(), other);
if (other.search(n.getData())) {
found = true;
}
sameTree(n.getRightNode(), other);
}
return found;
}
在主要方法中,我创建了2个BST并在其中插入了值。然后,我打电话给我以下称为方法:
System.out.println("Are the trees the same: " + tree1.same(tree2));
再次查看我的代码,你是对的,我会尝试再次解决它。谢谢! – Scarl