[leetcode]109 Convert Sorted List to Binary Search Tree

Source: Internet
Author: User

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

Http://fisherlei.blogspot.com/2013/01/leetcode-convert-sorted-list-to-binary.html

http://blog.csdn.net/worldwindjp/article/details/39722643

/** * definition for singly-linked list. * public class listnode  { *     int val; *     ListNode  Next; *     listnode (int x)  { val = x; next =  null; } * } *//** * Definition for binary tree *  public class treenode { *     int val; *      TreeNode left; *     TreeNode right; *      treenode (int x)  { val = x; } * } */public  class solution {    public treenode sortedlisttobst (ListNode  head)  {                 // option  A:        treenode root = buildtop2buttom (head,  NULL);        return root;    }     //////////////////////////    // Option A: Top to  bottom    //    // unlike sorted array, no  random access.    // but we still can follow the  Same strategy that:    // given head, find the mid.     // o (n log n)     private TreeNode  Buildtop2buttom (listnode start, listnode end)     {         if  (start == null | |  start == end)             return null;                     if  (start.next == end)              return new treenode (Start.val);                 listnode mid =  findmid (start, end);        treenode root =  New treenode (Mid.val);        root.left =  Buildtop2buttom (Start, mid);        root.right =  Buildtop2buttom (mid.next, end);        return root;     }        // find the mid node  from  ' start ' (inclusive)  TO  ' End ' (exclusive)     private listnode findmid (Listnode start, listnode  end)     {        listnode toreturn  = null;        ListNode s1 = start;         ListNode s2 = start;         while  (s2 != null && s2 != end)          {             toreturn = s1;            s1 =  s1.next;            s2 = s2.next;             if  (s2 == null | |  s2 == end)             s2 = null;             else             s2 = s2.next;        }         return toreturn;    }}



[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.