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;
    }
    }
  • 相关阅读:
    svn创建分支问题.未提交就创建分支
    中文繁简字对照表(继) 保留一简多繁的映射关系
    struts2配置json,警告:no result type defined for type 'json'
    eclipse更改修改编码方式的几种方法
    URLConnection简单爬虫(转)
    eclipse更改文件编码方式
    java学习笔记十——堆和栈的理解
    java学习笔记九——构造函数、方法重写、重载
    java学习笔记三——数据类型转换
    java学习笔记二——数据类型
  • 原文地址:https://www.cnblogs.com/RazerLu/p/3537445.html
Copyright © 2011-2022 走看看