I'm trying to check symmetry in a given tree, my idea was that i could make two Arraylists from the left subtree with pre order traversal(NLR) and from the right subtree with reverse preorder traversal(NRL), and then compare the two with equality operator and I would have a boolean value. I've checked the returning Arraylist values but it seems to always seem to give back false no matter any of the matching value in Arraylists. Any tips on why this is happening is appreciated.
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;
}
}
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 return false
.
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 use equals()
, you first need to override it in the BinaryTree
class, and provide a definition of equality between BinaryTree
objects. Note also that the documentation recommends overriding the hashcode()
method as well when overriding equals()
, 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 for equals()
, your traversal()
method would look more like this:
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).equals(traversal(root.right, false));
}