Convert Sorted List to Binary Search Tree

来源:互联网 发布:java设置编码格式 编辑:程序博客网 时间:2024/06/05 07:58

Problem : Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.

SOLUTION 1:  TOP DOWN (NlogN)

Similar to converting sorted array to binary search tree. First, we find the root of the binary search tree, that is, in the middle of the array. It is very easy to find the root in an array. But for an linked list, we need O(n) time to to locate the root. So the total work is NlogN.

RECURSION PARTS LIKE:

ListNode root = findTootOfBST(ListNode start); // this takes O(N) time

TreeNode root = new TreeNode(rootNode.val);

root.right = builder(rootNode.next.next);

rootNode.next = null;

root.left = builder(start)

A naive way is to apply the previous solution directly. In each recursive call, you would have to traverse half of the list’s length to find the middle element. The run time complexity is clearlyO(N lg N), where N is the total number of elements in the list. This is because each level of recursive call requires a total ofN/2 traversal steps in the list, and there are a total of lg N number of levels (ie, the height of the balanced tree).

SOLUTION 2: BOTTOM  UP (logN)

As usual, the best solution requires you to think from another perspective. In other words, we no longer create nodes in the tree using the top-down approach. 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.

How we come up with a bottom up solution? 

For linkedList, the natural way to access it is one node after one node. So if we create left child before we create the root child, we are guaranteed that each time we reached the root for this level, ListNode pointer current are pointing to the root. This is also why we need to make the variable currentglobal. 

/** * Definition for ListNode. * public class ListNode { *     int val; *     ListNode next; *     ListNode(int val) { *         this.val = val; *         this.next = null; *     } * } * Definition of TreeNode: * public class TreeNode { *     public int val; *     public TreeNode left, right; *     public TreeNode(int val) { *         this.val = val; *         this.left = this.right = null; *     } * } */ public class Solution {    /**     * @param head: The first node of linked list.     * @return: a tree node     */    ListNode cur;    public TreeNode sortedListToBST(ListNode head) {          // write your code here        if (head == null) {            return null;        }        cur = head;        int size = 0;        ListNode p = head;        while (p != null) {            size++;            p = p.next;        }        return builder(size);    }    TreeNode builder(int size) {        if (size <=0) {            return null;        }        TreeNode left = builder(size / 2);        TreeNode root = new TreeNode(cur.val);        cur = cur.next;        TreeNode right = builder(size - 1 - size / 2);        root.left = left;        root.right = right;        return root;    }}



0 0
原创粉丝点击