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;
    }
    }
  • 相关阅读:
    springboot 无法访问静态资源
    webrtc源码阅读理解一
    c++ include的顺序重要吗?
    简说yuv
    i420 转 nv21
    ffmpeg mp4 转 yuv、 y4m转yuv、mp4转y4m && ffplay 播放y4m、yuv
    mysql 查询json字符串条件
    Webpack4.X中sourcemap的配置 详解
    webpack如何打包生成的map文件不生效?
    vue中使用setInterval,移入暂停,移出继续
  • 原文地址:https://www.cnblogs.com/RazerLu/p/3537445.html
Copyright © 2011-2022 走看看