1

i am confused because I need my array to be equal to the other array but I don't know how to compare them without losing their values

josh mc
  • 13
  • 3
  • Does this answer your question? [How do I generate random integers within a specific range in Java?](https://stackoverflow.com/questions/363681/how-do-i-generate-random-integers-within-a-specific-range-in-java) – josh mc Nov 22 '19 at 04:03

2 Answers2

2

If both roots are null you will get an undesired result based on what you're trying to do with your second if condition.

It looks like if both roots are null you want to return true, but you're returning false. You could use just one if statement

if(thisRoot == null || otherRoot == null){
  return thisRoot == null && otherRoot == null;
}

You have a bigger problem with how you're comparing the data of the two nodes.

thisRoot.getData() != otherRoot.getData()

This comparison is not what I think you're looking for. Instead you should overrride the equals method for your data objects and compare using it instead

JBurk94
  • 115
  • 1
  • 6
  • thanks man! and I was wondering, is there any way to do the long return recursion step at the bottom not as lengthy or a way to chop it up so it looks cleaner? – josh mc Nov 11 '19 at 21:15
1

The order of your conditions causes a problem.

if (thisRoot == null || otherRoot == null) {
    return false;
}
if (thisRoot == null && otherRoot == null) {
    return true;
}

The first condition will evaluate to true (and lead to return false) even if both branches are null. You should first evaluate if both branches are null; after that, you can check the case where only one of them is null.