[leetcode#109] Convert Sorted List to Binary Search Tree

Source: Internet
Author: User

The problem:

Convert Sorted List to Binary Search Tree

Link:

https://oj.leetcode.com/problems/convert-sorted-list-to-binary-search-tree/

My Analysis:

The idea of building a binary tree from a sorted linked list was same as from a sorted array.
The key issues here is:
1. Locate the mid element of linked list
2. Split the linked list into the parts according mid element.

The classic-the-finding the mid element on a linked list was to use both Pointers:walker and runner.

 while NULL NULL ) {                        = Walker;             = Walker.next;             = Runner.next.next;}

Note:the mid element would is the node pointer by Walker, to split the list into the parts, we had to keep a pre_walker Pointer to point the previous element of Walker.

One pitfall.
The corner Case:when There is only a elements in the list.
In this situation, we take the first element as root and the second element as the right child.
In this case, the above invariant could isn't be used, we should tackle it specically. And, we could detect this case by testing if Pre_walker have already been initialized.

if NULL ) {    nullElse  {     = head;     NULL ; }

Then the problem was fixed!

My Solution:

 Public classSolution { PublicTreeNode Sortedlisttobst (ListNode head) {if(Head = =NULL)            return NULL; returnHelper (head); }        PrivateTreeNode Helper (ListNode head) {if(Head = =NULL)            return NULL; if(Head.next = =NULL)            return NewTreeNode (Head.val);        ListNode Head1;        ListNode head2; ListNode Walker=Head; ListNode Runner=Head; ListNode Pre_walker=NULL;//Use to record the pre node of Walker pointerListNode mid;  while(Runner.next! =NULL&& Runner.next.next! =NULL) {Pre_walker=Walker; Walker=Walker.next; Runner=Runner.next.next; } Mid= Walker;//The mid position is pointed by WalkerTreeNode ret =NewTreeNode (Mid.val); /*if Pre_walker has already been initialized, there is at list three elements the list. */        if(Pre_walker = =NULL) {//careful! only and elements left, we choose the head as mid element.Head1 =NULL; } Else{head1=Head; Pre_walker.next=NULL;//split the original linked list at Walker.} head2=Mid.next; Ret.left=Helper (HEAD1); Ret.right=Helper (head2); returnret; }}

[leetcode#109] Convert Sorted List to Binary Search Tree

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.