zoukankan      html  css  js  c++  java
  • 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.

    Ref: http://www.cnblogs.com/feiling/p/3267917.html

    top down 把linkedlist 中的node 的值 添加到 一个array中去

    /**
     * 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) {
            ListNode p = head;
            int count = 0;
            while(p != null){
                count++;
                p = p.next;
            }
            
            int[] num = new int[count];
            p = head;
            int i = 0;
            while(p != null){
                num[i++] = p.val; 
                p = p.next;
            }
            
            return generate(0, num.length-1, num);
        }
        
        private TreeNode generate(int start, int end, int[] num){
            if(start > end) 
                return null;
            
            int mid = (start + end)/2;
            TreeNode root = new TreeNode(num[mid]);
            root.left = generate(start, mid-1, num);
            root.right = generate(mid+1, end, num);
            return root;
        }
    }

    bottom up(TODO).

    Best Solution:
     We create nodes bottom-up, and assign them to its parents. The bottom-up approach enables us to access the list in its order while creating nodes.

    Although bottom-up approach is not the most natural way we think, it is extremely helpful in some cases. However, you should prefer top-down instead of bottom-up in general, since the latter is more difficult to verify in correctness.

     Please note that the algorithm requires the list’s length to be passed in as the function’s parameters. The list’s length could be found in O(N) time by traversing the entire list’s once. The recursive calls traverse the list and create tree’s nodes by the list’s order, which also takes O(N) time. Therefore, the overall run time complexity is still O(N).

    But things get a little more complicated when you have a singly linked list instead of an array. Now you no longer have random access to an element in O(1) time. Therefore, you need to create nodes bottom-up, and assign them to its parents. The bottom-up approach enables us to access the list in its order at the same time as creating nodes.

    /**
     * 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) {
        // calculate list length
        int len = 0; ListNode cur = head;
        while (cur!=null) {
            cur = cur.next;
            len++;
        }
        // build the BST
        return listToBST(head, 0, len-1);
    }
    
    private TreeNode listToBST(ListNode head, int low, int high) {
        if (low > high) return null;
        int mid = (low + high) / 2;
        // build up tree recursively
        TreeNode left = listToBST(head, low, mid-1);
        TreeNode root = new TreeNode(head.val);
        root.left = left;
        // Java pass in Object by reference, so we can't change head but we can change its content :)
        if (head.next != null) { // "move to next"
            head.val = head.next.val;
            head.next = head.next.next;
        }
        root.right = listToBST(head, mid+1, high);
        return root;
    }
    }
  • 相关阅读:
    关于Web 打印的三种方法
    禁止另存为 禁止复制 禁止右键 JS
    无限数据递归
    VS2003+Ajax Div文本框输入提示
    C#关于日期月天数和一年有多少周及某年某周时间段的计算
    git代码冲突
    RabbitMQ死信队列
    Elementui:ElContainer布局
    mxml调用as
    用自己的MapServer,解决沙箱冲突
  • 原文地址:https://www.cnblogs.com/RazerLu/p/3537445.html
Copyright © 2011-2022 走看看