I am trying to write this function that takes in a doublyLinkedList and constructs a balanced binary search tree in place. The TreeNode.left is equivalent to the previous pointer and TreeNode.right is like the next pointer. I am taking inspiration from the program here but that doesnt work:
http://www.geeksforgeeks.org/in-place-conversion-of-sorted-dll-to-balanced-bst/
private static TreeNode constructBST2(TreeNode head, int m, int n) {
TreeNode temp = null;
if (m < n) {
int mid = m + (n - m)/ 2;
TreeNode left = constructBST2(head, m, mid);
temp = head;
temp.left = left;
head = head.right;
temp.right = constructBST2(head, mid + 1, n);
}
return temp;
}