I'm getting this error while submitting the code, but this is working fine while custom input test.
why i'm getting out of bound error while submission.
Question.
99. Recover Binary Search Tree.
here is my code
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
static int a=0;
public void recoverTree(TreeNode root) {
List<Integer> l1 = new ArrayList<>();
TreeNode r2 = root;
trvrs(r2,l1);
// System.out.println(l1);
Collections.sort(l1);
// System.out.println(l1);
recover(root,l1);
}
public static void trvrs(TreeNode root, List<Integer> l)
{
if(root==null) return ;
trvrs(root.left,l);
l.add(root.val);
trvrs(root.right,l);
}
public static void recover(TreeNode root, List<Integer> l)
{
if(root==null) return ;
recover(root.left,l);
if(root!=null && root.val != l.get(a))
{
// System.out.println(root.val+" ___ "+l.get(a));
root.val = l.get(a);
a++;
}else{
a++;
}
recover(root.right,l);
}
}