• 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;
    }
    }
  • 相关阅读:
    03、使用字符串
    加载selenium2Library失败---robotframework环境搭建(site-packages下无selenium2library文件夹)
    python无法启动火狐浏览器且报错“selenium.common.exceptions.WebDriverException: Message: Unable to find a matching set of capabilities”
    移动H5前端性能优化指南
    appium+python 启动一个app步骤
    Appium_Python_Client介绍
    python自动化---各类发送邮件方法及其可能的错误
    python自动化--批量执行测试之生成报告
    揭秘webdriver实现原理【转】
    selenium 三种断言以及异常类型
  • 原文地址:https://www.cnblogs.com/RazerLu/p/3537445.html
Copyright © 2020-2023  润新知