Hi there,
I don't know if LeetCode runs the same tests for every language but I've tried 3 different algorithms for this in Java and they all fail exactly in the same point which is arr[5] = 7 and node with value =7, left = 6 and right = 0.
This is the test case, that is supposed to return true.
arr = [0,0,6,9,9,7,4,6,2,8,9,4,5,7,3,8]
And this is the tree in a bfs arr =
[0,9,0,5,6,6,9,2,8,1,6,9,5,6,3,1,4,1,9,9,1,0,1,9,7,0,4,6,5,2,7,3,3,6,9,8,2,9,1,8,5,9,2,-1,5,3,4,7,6,5,3,2,7,6,4,0,2,0,5,8,4,1,2,9,0,-1,2,7,8,7,4,9,-1,9,3,9,7,0,7,3,7,-1,7,3,5,4,1,1,8,-1,7,7,9,4,2,6,0,-1,5,5,4,1,0,7,4,9,8,2,8,5,2,-1,-1,1,9,0,5,7,3,-1,-1,9,4,3,6,2,9,1,1,8,5,0,-1,8,-1,6,8,4,5,2,3,-1,-1,-1,-1,0,-1,2,9,1,-1,-1,-1,8,-1,7,-1,1,1,-1,5,8,9,5,6,-1,4,5,9,-1,4,6,-1,-1,1,8,-1,6,3,4,5,7,3,3,9,8,-1,0,1,3,9,9,0,4,3,1,3,9,2,1,9,5,-1,1,8,3,6,-1,-1,5,3,-1,2,2,4,6,9,8,2,-1,5,2,-1,-1,4,1,6,-1,9,5,8,3,-1,-1,8,3,-1,7,6,-1,6,4,-1,-1,-1,-1,-1,7,0,9,4,6,5,2,3,-1,0,2,1,-1,-1,8,3,-1,-1,-1,-1,-1,-1,1,0,-1,-1,-1,4,-1,9,-1,7,-1,-1,5,7,-1,2,-1,0,-1,5,-1,-1,-1,-1,-1,0,4,-1,2,3,2,-1,5,-1,1,5,3,-1,-1,6,2,4,1,9,9,3,-1,-1,3,9,0,-1,7,7,2,8,9,8,8,2,6,9,4,6,3,0,5,0,8,-1,-1,5,5,6,3,0,6,9,0,0,1,0,1,0,7,2,-1,-1,-1,-1,-1,-1,-1,-1,0,6,8,4,-1,5,3,0,9,-1,-1,-1,-1,5,7,9,0,8,4,6,3,5,9,-1,-1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,-1,0,8,-1,-1,3,-1,-1,-1,-1,-1,-1,-1,-1,-1,3,-1,-1,-1,-1,-1,-1,-1,-1,5,-1,-1,-1,-1,9,-1,5,-1,4,-1,-1,-1,-1,-1,-1,-1,2,-1,-1,-1,7,-1,-1,1,5,7,8,-1,-1,8,-1,-1,1,-1,3,-1,-1,4,6,-1,-1,9,-1,-1,-1,1,2,4,-1,1,1,-1,-1,3,-1,4,3,-1,-1,-1,5,6,0,6,4,3,8,-1,9,-1,-1,-1,9,-1,-1,-1,0,7,-1,-1,3,-1,9,8,1,2,7,7,-1,-1,4,-1,6,8,3,9,-1,-1,2,-1,-1,8,-1,-1,-1,8,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,5,-1,-1,7,-1,-1,-1,-1,-1,-1,-1,-1,-1,5,-1,-1,-1,-1,0,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,4,-1,3,-1,8,2,0,-1,-1,-1,-1,0,-1,-1,6,-1,-1,-1,7,-1,-1,8,3,0,-1,-1,-1,-1,-1,-1,4,4,-1,-1,-1,-1,1,-1,-1,3,-1,-1,2,-1,5,8,-1,-1,-1,-1,-1,-1,-1,-1,-1,7,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,2,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,3,8,3,5,-1,-1,-1,4,-1,-1,8,-1,0,0,-1,2,-1,1,-1,7,-1,5,9,2,-1,-1,-1,9,3,0,3,-1,-1,-1,-1,6,0,6,-1,5,8,-1,7,7,-1,-1,-1,-1,-1,2,4,9,-1,-1,-1,-1,-1,5,-1,6,-1,-1,-1,-1,-1,-1,5,-1,-1,-1,-1,-1,-1,8,-1,9]
Please note: -1 are null nodes
My solution #1:
public boolean isValidSequence2(TreeNode root, int[] arr) {
TreeNode node = root;
if (arr[0] != node.val) return false;
for (int i = 1; i < arr.length; i++) {
if (node.left != null && node.left.val == arr[i]) node = node.left;
else if (node.right != null && node.right.val == arr[i]) node = node.right;
else return false;
}
return node.left == null && node.right == null;
}My solution #2:
public boolean isValidSequence(TreeNode root, int[] arr) {
return dfsCheck(root, arr, 0);
}
boolean dfsCheck(TreeNode root, int[] arr, int index) {
if (root.val != arr[index]) return false;
if (index < arr.length - 1) {
if (root.left != null && root.left.val == arr[index + 1]) return dfsCheck(root.left, arr, ++index);
else if (root.right != null && root.right.val == arr[index + 1]) return dfsCheck(root.right, arr, ++index);
else return false;
}
if (index == arr.length - 1) {
if (root.left != null || root.right != null) return false;
}
return true;
}
What am I doing wrong?
Thank you for your comments