首页 > 其他 > 详细

Jan 28 - Convert Sorted List To BST; Linked List; Tree; DFS; DAC;

时间:2016-01-29 12:02:36      阅读:189      评论:0      收藏:0      [点我收藏+]

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;
    }
}

 

Jan 28 - Convert Sorted List To BST; Linked List; Tree; DFS; DAC;

原文:http://www.cnblogs.com/5683yue/p/5168347.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!