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;
}
}
CodePudding user response:
return traversal(root.left, true) == traversal(root.right, false);
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:
var arr1 = List.of(1,2,3);
var arr2 = List.of(1,2,3);
arr1 == arr2 // this is false because the actual references are different
arr1.equals(arr2) // this yields "true"
CodePudding user response:
The reason why your confrontation yields false is because you're confronting two reference type with the ==
operator. A reference contains the address of the object it points to while the ==
operator confronts the content of two variables. Since you're confronting two references, what you're doing is checking whether the address contained by the first reference is equal to the address contained by the second reference, since they both point to different objects in memory, and therefore to different memory addresses, this confrontation can only return false.
If you want to check if these two references represent the same content, then you need to use the equals
method. However, the equals
method of a Collection
simply invokes the same equals
method on its elements, so since you're using a generic type for your BinaryTree
class, you need to warn the user of your class that the provided argument type should have a proper equals
implementation.
Getting back to your traversal
method, it should look like this:
public static boolean symmetricTree(BinaryTree<Integer> root){
System.out.println(traversal(root.left, true));
System.out.println(traversal(root.right, false));
//Use equals to confront reference type, the == operator is only for primitive types
return traversal(root.left, true).equals(traversal(root.right, false));
}