Convert Sorted List to Binary Search Tree

Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.

这道题和之前的将sorted array做成BST一样,那道题是找mid,这道题也是找mid,只不过linkedlist里面的mid是用两个指针,一个fast和一个slow找,另外注意我们找的是mid的前一个点,这样可以将list从mid分成两个list再recurse。 将mid设为根节点以后,要把mid之前的那个节点设为null,mid之后的那个节点作为右边子树的head,然后在recursive的形成树即可。

/**
 * 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) {
        if (head == null)
            return null;
        if (head.next == null) // only one node   
        {
            TreeNode t = new TreeNode(head.val);
            return t;
        }
        else if (head.next.next == null) // two node
        {
            TreeNode t = new TreeNode(head.next.val);
            t.left = new TreeNode(head.val);
            return t;
        }
        else //three or more nodes
        {
            ListNode m = findMid(head);
            TreeNode t = new TreeNode(m.next.val); // head
            ListNode h = m.next; // new head for right sub list
            m.next = null;
            t.left = sortedListToBST(head);
            t.right = sortedListToBST(h.next);
            return t;
        }
    }
    
    public ListNode findMid(ListNode head)
    {
        ListNode slow = head;
        ListNode last = slow;
        ListNode fast = head;
        while(fast != null)
        {
            fast = fast.next;
            if (fast == null)
                break;
            fast = fast.next;
            last = slow;
            slow = slow.next;
        }
        return last; // return one node before the mid
    }
}

Keep track of the length of the list so that we can use merge sort like method to build a tree

/**
 * 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 {
    ListNode iter;
    public TreeNode sortedListToBST(ListNode head) {
        if (head == null)
            return null;
        if (head.next == null)
            return new TreeNode(head.val);
        int len = 0;
        ListNode h = head;
        while(h != null) {
            len++;
            h = h.next;
        }
        iter = head;
        return buildTree(len);
    }
    
    public TreeNode buildTree(int len) {
        if (len == 0)
            return null;
        if (len == 1) {
            TreeNode root = new TreeNode(iter.val);
            iter = iter.next;
            return root;
        }
        TreeNode left = buildTree(len/2);
        TreeNode root = new TreeNode(iter.val);
        iter = iter.next;
        TreeNode right = buildTree(len - len/2 - 1);
        root.left = left;
        root.right = right;
        return root;
    }
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s