• Is it possible to provide more feedback than simply "Runtime error" maybe the first few lines of the stack trace?

For some reason the code below is failing on test input

{2,3,3,4,#,#,4,#,5,5,#,#,6,6,#,7,8,8,7,9,0,0,1,1,0,0,9}

I have tried to runt this through a unit test and the code passes my side. It could very well be because I am mis-interpreting the tree syntax notation.

Anyway here is my code below. Would be great to be able to see solutions.

``````public class Solution {
public boolean isSymmetric(TreeNode root) {
TreeNode[] array = new TreeNode[1];
array[0] = root;
return this.testSymmetric(0, array);
}

private boolean testSymmetric(int depth, TreeNode[] currentArray) {
if (this.isNull(currentArray)) {
return true;
}
TreeNode[] tmpArray = new TreeNode[(int) Math.pow(2, depth + 1)];
if (currentArray.length > 1) {
int start = 0;
int end = tmpArray.length - 1;
//if (currentArray.length%2!=0) return false;
int mid = currentArray.length / 2;
for (int i = 0; i < mid; i++) {
if (currentArray[0 + i] != null && currentArray[currentArray.length - i - 1] != null) {
if (currentArray[0 + i].val != currentArray[currentArray.length - i - 1].val) {
return false;
};
tmpArray[start + (int) Math.pow(2, i) - 1] = currentArray[0 + i].left;
tmpArray[start + (int) Math.pow(2, i)] = currentArray[0 + i].right;
tmpArray[end - (int) Math.pow(2, i)] = currentArray[currentArray.length - i - 1].left;
tmpArray[end - (int) Math.pow(2, i) + 1] = currentArray[currentArray.length - i - 1].right;
} else {
if (currentArray[0 + i] != null || currentArray[currentArray.length - i - 1] != null) {
return false;
}
}
}
depth++;
return testSymmetric(depth, tmpArray);
} else {
tmpArray[0] = currentArray[0].left;
tmpArray[1] = currentArray[0].right;
depth++;
return testSymmetric(depth, tmpArray);
}
}

private boolean isNull(TreeNode[] array) {
for (int i = 0; i < array.length; i++) {
if (array[i] != null) {
return false;
}
}
return true;
}

}``````

• The tree interpretation is only for constructing the test case input tree.
Although, if you've correctly mentioned the input here, then I believe the notation is indeed incorrect (since, it's mentioned in question, OJ tree representation is based on level-order traversal).

However, I'd suggest resorting to recursive approaches for problems as this (unless, explicitly called out to use iterative).

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.