Trying to sort [5,3,9,-2147483648,2] using comparator where the values are TreeNodes
Structure of TreeNode:
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;
}
}
Below is my code :
Collections.sort(list,new Comparator<>(){
public int compare(TreeNode a,TreeNode b)
{
return a.val-b.val;
}
});
Here list is collection of TreeNodes
Actual Output : 2,3,5,9,-2147483648
Expected Output : -2147483648,2,3,5,9