Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Discussion of Leetcode 109 with the peer from 6:00 PM - 7:30 PM
private ListNode node; <- linked list
public TreeNode sortedListToBST(ListNode head) {
if(head == null){
return null;
}
int size = 0;
ListNode runner = head;
node = head;
while(runner != null){ // 152 - 155 - travel list - find how many nodes
runner = runner.next;
size ++;
}// here we get size...
return inorderHelper(0, size - 1);
}
public TreeNode inorderHelper(int start, int end){
if(start > end){
return null;
}
int mid = start + (end - start) / 2;
TreeNode left = inorderHelper(start, mid - 1);
TreeNode treenode = new TreeNode(node.val); // <- if there is only one, put node definitely to our root node <-
treenode.left = left;
node = node.next; // <- move linked list to next node <- mapping inorder traversal Left, root, right , only have one node
TreeNode right = inorderHelper(mid + 1, end);
treenode.right = right;
return treenode;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment