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

链接: http://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/

题解:

值得思考的一道题。一开始的想法是跟convert sorted array to BST一样,用快慢指针找到中点,然后自顶向下返回构建BST。这样的话Time Complexity - O(nlogn), Space Complexity - O(n)。 再一想为什么不干脆吧遍历list的时候创建一个数组,然后用数组重建BST,这样Time Complexity - O(n), Space Complexity - O(n)。 不过另外建立一个数组这事情做的有点丑。后来看了discuss版里1337大神说要自底向上构建,这样就不用每次找中点了。方法是in-order traversal + DFS自底向上,很巧妙。 以后假如自顶向下不好实现的话不妨试一试自底向上。不过in general一般还是用自顶向下。下面是实现:

Time Complexity - O(n),Space Complexity - O(n)

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    private ListNode current;
    public TreeNode sortedListToBST(ListNode head) {
        if(head == null)
            return null;
        int listSize = 0;
        current = head;
        ListNode node = head;
while(node != null) { node = node.next; listSize++; } return sortedListToBST(listSize); } private TreeNode sortedListToBST(int listSize) { // in-order traversal if(listSize <= 0) return null; TreeNode left = sortedListToBST(listSize / 2); TreeNode root = new TreeNode(current.val); current = current.next; TreeNode right = sortedListToBST(listSize - 1 - listSize / 2); root.left = left; root.right = right; return root; } }

 

二刷:

看不懂楼上在写什么。这次还是使用了找中点再自顶向下递归构建BST的方法。注意当slow == head的时候,我们的左子树为null,否则可以递归调用原方法求解。

Java:

Time Complexity - O(nlogn),Space Complexity - O(n)

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
/**
 * Definition for a binary tree node.
 * 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;
        ListNode fast = head, slow = head, pre = head;
        while (fast != null && fast.next != null) {
            pre = slow;
            fast = fast.next.next;
            slow = slow.next;
        }
        pre.next = null;
        TreeNode root = new TreeNode(slow.val);
        root.left = (head != slow) ? sortedListToBST(head) : null;
        root.right = sortedListToBST(slow.next);
        return root;
    }
}

 

再仔细读了一下1337c0d3r的article

使用In-order traversal。

  1. 我们要先设置一个global的ListNode
  2. 遍历head取得list长度
  3. 构建辅助方法,使用in-order traversal来递归构建树

Time Complexity - O(n),Space Complexity - O(n)

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    ListNode cur;
    public TreeNode sortedListToBST(ListNode head) {
        if (head == null) return null;
        ListNode node = head;
        int count = 0;
        while (node != null) {
            node = node.next;
            count++;
        }
        cur = head;
        return sortedListToBST(0, count - 1);
    }
    
    private TreeNode sortedListToBST(int lo, int hi) {
        if (lo > hi) return null;
        int mid = lo + (hi - lo)/ 2;
        TreeNode left = sortedListToBST(lo, mid - 1);
        TreeNode root = new TreeNode(cur.val);
        cur = cur.next;
        TreeNode right = sortedListToBST(mid + 1, hi);
        root.left = left;
        root.right = right;
        return root;
    }
}

 

 

Reference:

http://articles.leetcode.com/2010/11/convert-sorted-list-to-balanced-binary.html

https://leetcode.com/discuss/83856/share-my-java-solution-1ms-very-short-and-concise

https://leetcode.com/discuss/14113/sorted-list-to-binary-search-tree-o-nlogn-time-complexity

https://leetcode.com/discuss/10924/share-my-code-with-o-n-time-and-o-1-space

https://leetcode.com/discuss/23676/share-my-o-1-space-and-o-n-time-java-code

https://leetcode.com/discuss/19688/my-accepted-c-solution

https://leetcode.com/discuss/58428/recursive-construction-using-slow-fast-traversal-linked-list

https://leetcode.com/discuss/3810/can-someone-do-this-in-place-with-o-n-time

https://leetcode.com/discuss/50818/java-solution-using-two-pointer-technique

posted @ 2015-04-18 13:54  YRB  阅读(1360)  评论(0编辑  收藏  举报