Using the same DAC idea of previous problem. Difference is that we need to do the pointed operation of linked list rather than just change the index of array. If we find the middle position of the list, get the value of the list node, and get the list node (prev) ahead of it and then set prev.next = null. Then the left child node should be the in the remaining list ahead of current list node. Similarly, the right child node should be found in the left list part of current list node.

Code:

/**
 * 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) {
        ListNode cur = head;
        if(cur == null) return null;
        int val = cur.val;
        TreeNode node = new TreeNode(val);
        if(cur.next == null) return node;
        if(cur.next.next == null) node.right = sortedListToBST(cur.next);
        else{
            int i = 0;
            while(cur != null){
                cur = cur.next;
                i++;
            }
            int j = 0;
            cur = head;
            ListNode prev = head;
            while(j < i/2){
                prev = cur;
                cur = cur.next;
                j++;
            }
            val = cur.val;
            node = new TreeNode(val);
            prev.next = null;
            node.left = sortedListToBST(head);
            node.right = sortedListToBST(cur.next);
        }
        return node;
    }
}

 Here is the improvement: When we look through the current linked list from the head to the last node, in the end we can find the middle node the node ahead of middle node at the same time.

 

Code:

/**
 * 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) {
        ListNode cur = head;
        
        if(cur == null) return null;
        int val = cur.val;
        TreeNode node = new TreeNode(val);
        if(cur.next == null) return node;
        if(cur.next.next == null) node.right = sortedListToBST(cur.next);
        else{
            ListNode half = head;
            ListNode prev = head;
            while(cur != null && cur.next != null){
                prev = half;
                half = half.next;
                cur = cur.next.next;
            }
            val = half.val;
            node = new TreeNode(val);
            prev.next = null;
            node.left = sortedListToBST(head);
            node.right = sortedListToBST(half.next);
        }
        return node;
    }
}

 

posted on 2016-01-29 11:06  爱推理的骑士  阅读(157)  评论(0编辑  收藏  举报