Convert Sorted List to Binary Search Tree

11 篇文章 0 订阅
6 篇文章 0 订阅

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 clearly O(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 of N/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 current global. 

/**
 * 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值