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;
    }
    }
  • 相关阅读:
    Java Jaxb JavaBean与XML互转
    中国版Azure支持那些版本号Linux
    Codeforces 8D Two Friends 三分+二分+计算几何
    技术走向管理一些思考(3)-知识工作者的管理
    分析USB平台设备模型框架(1)
    (42)JS运动之多物体框架--多个div变宽
    听写英语单词的小程序
    一个基于node express4.0和mongodb的活动报名
    c编程:求出4×4矩阵中最大和最小元素值及其所在行下标和列下标,求出两条主对角线元素之和。
    重载()运算符和重载强制类型转换
  • 原文地址:https://www.cnblogs.com/RazerLu/p/3537445.html
Copyright © 2011-2022 走看看