在二进制树上检查对称性
我正在尝试在给定的树上检查对称性,我的想法是,我可以使用前订单遍历(NLR)从左子树中制作两个阵列列表,并从右侧子树和反向预订遍历(NRL)中进行比较,然后比较两个有了平等运算符,我将具有布尔值。 我已经检查了返回的ArrayList值,但是无论ArrayLists中的任何匹配值如何,它似乎总是会归还False。 关于为什么会发生这种情况的任何提示,都将受到赞赏。
import java.util.ArrayList;
class BinaryTree<E>{
public E data;
public BinaryTree<E> left;
public BinaryTree<E> right;
public BinaryTree() {}
public BinaryTree(E data) { this.data = data; }
public BinaryTree(E data, BinaryTree<E> left, BinaryTree<E> right) {
this.data = data;
this.left = left;
this.right = right;
}
}
class Solution{
public static boolean symmetricTree(BinaryTree<Integer> root){
System.out.println(traversal(root.left, true));
System.out.println(traversal(root.right, false));
return traversal(root.left, true) == traversal(root.right, false);
}
public static ArrayList<Integer> traversal(BinaryTree<Integer> root, boolean left){
// 関数を完成させてください
ArrayList<Integer> dArr = new ArrayList<>();
if(left) dArr = preorderTraversalHelper(root, dArr);
else dArr = reversePreorderTraversalHelper(root, dArr);
//System.out.println(dArr);
return dArr;
}
public static ArrayList<Integer> preorderTraversalHelper(BinaryTree<Integer> root, ArrayList<Integer> dArr){
if (root == null) return null;
dArr.add(root.data);
preorderTraversalHelper(root.left, dArr);
preorderTraversalHelper(root.right, dArr);
return dArr;
}
public static ArrayList<Integer> reversePreorderTraversalHelper(BinaryTree<Integer> root, ArrayList<Integer> dArr){
if (root == null) return null;
dArr.add(root.data);
reversePreorderTraversalHelper(root.right, dArr);
reversePreorderTraversalHelper(root.left, dArr);
return dArr;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的对抗产生
false
的原因是因为您正在与==
操作员面对两个引用。参考包含指向对象的地址,而
==
操作员面对两个变量的 content 。因此,您实际上在代码中所做的是检查第一个参考所包含的地址是否等于第二个参考文献所包含的地址。但是,由于两个引用都指向不同的对象,因此要进行不同的内存地址,因此对抗只能返回false
。如果要检查两个引用是否指向两个代表相同内容的对象,则需要使用 equals(),首先需要在
binarytree
类中覆盖它,并提供binarytree
对象之间的平等定义。还要注意,该文档建议在覆盖equals()
时也建议覆盖hashCode()
方法,以便这两种方法都与 hashcode方法的一般合同 >平等对象必须具有相等的哈希代码。但是,这只是一个建议,在您的情况下,这不是严格必要的。假设
BinaryTree
为equals()
提供了适当的定义,那么您的traversal()
方法看起来更像是这样:The reason why your confrontation yields
false
is because you're confronting two references with the==
operator.A reference contains the address of the object it points to, while the
==
operator confronts the content of two variables. So, what you're actually doing in your code is checking if the address contained by the first reference is equal to the address contained by the second reference. However, since both references point to different objects, and therefore different memory addresses, the confrontation can only returnfalse
.If you want to check if two references point to two objects representing the same content, you need to use the
equals()
method. In order to useequals()
, you first need to override it in theBinaryTree
class, and provide a definition of equality betweenBinaryTree
objects. Note also that the documentation recommends overriding thehashcode()
method as well when overridingequals()
, so that both methods are consistent with the general contract for the hashCode method, which states that equal objects must have equal hash codes. However, this is just a recommendation and is not strictly necessary in your case.Assuming that
BinaryTree
provides a proper definition forequals()
, yourtraversal()
method would look more like this:这听起来对我来说是错误的,在Java中,有一个
等于
方法,该方法实际上检查了数组是否平等(以相同的顺序具有相同的元素)。您介绍的方式比较了参考文献:This sounds wrong to me, in Java there is an
equals
method that actually checks that the arrays are equals (have the same elements in the same order). The way you've presented compares the references: