108. Convert Sorted Array to Binary Search Tree (building tree with resursion)

Given an array where elements are sorted in ascending order, convert it to a height balanced BST.

For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.

Example:

Given the sorted array: [-10,-3,0,5,9],

One possible answer is: [0,-3,9,-10,null,5], which represents the following height balanced BST:

      0
     / \
   -3   9
   /   /
 -10  5

Solution: O(n) , space: 栈空间O(logn)(from recusrsive expression)加上结果的空间O(n) : O(n) (good reference: https://blog.csdn.net/linhuanmars/article/details/23904883)

  • sorting array for BST(left < root < right)
  • start from middle node and let left part as left subtree , right as well
  • recursion with returing root-- pattern:
TreeNode root = new TreeNode(nums[m]);
root.left = helper(nums, l, m-1);
root.right = helper(nums, m+1, r);
return root;

Totally

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
   
    public TreeNode sortedArrayToBST(int[] nums) {
        if(nums.length == 0) return null;
        return helper(nums, 0, nums.length-1);
    }
    // //recursive with return, 
    TreeNode helper(int[] nums, int l, int r){
        if(l > r) return null;
        int m = (r-l)/2 + l;
        TreeNode root = new TreeNode(nums[m]);
        root.left = helper(nums, l, m-1);
        root.right = helper(nums, m+1, r);
        return root;
    }
}

 

Follow up questions: 109 convert sorted list to BST

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

For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.

Example:

Given the sorted linked list: [-10,-3,0,5,9],

One possible answer is: [0,-3,9,-10,null,5], which represents the following height balanced BST:

      0
     / \
   -3   9
   /   /
 -10  5

Solution: 

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    //one way: get middle of linkedlist (slow, fast)
    //another way: use preorder(left, root, right), need get the number of anodes in the list
    //1: int m = (r-l)/2 + l; 2: //node just copy the reference
    public TreeNode sortedListToBST(ListNode head) {
        if(head == null) return null;
        ListNode cur = head;
        int m = 0;
        while(cur != null){
            m++;
            cur = cur.next;
        }
        List<ListNode> list = new ArrayList<>();
        list.add(head);
        return helper(list, 0, m-1);
    }
    TreeNode helper(List<ListNode> list, int l, int r){ //node just copy the reference
        if(l>r) return null;
        
        int m = (r-l)/2 + l;
        TreeNode left = helper(list, l, m-1);//
        TreeNode root = new TreeNode(list.get(0).val);
        root.left = left;
        list.set(0, list.get(0).next);
        root.right = helper(list, m+1, r);
        return root;
    }
    
    
    
}

Solution 2: get middle of list (slow and fast)

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public TreeNode sortedListToBST(ListNode head) {
        if(head == null) return null;
        return BST(head, null);
    }
    public TreeNode BST(ListNode head, ListNode tail) {
        if(head == tail) return null;
        
        ListNode slow = head;
        ListNode fast = head;
        while(fast!=tail&&fast.next!=tail) {  //tail
            fast = fast.next.next;
            slow = slow.next;
        }
        TreeNode node = new TreeNode(slow.val); 
        node.left = BST(head, slow);
        node.right = BST(slow.next, tail);
        return node;
    }
}

 

posted @ 2018-08-10 15:11  wz30  阅读(174)  评论(0编辑  收藏  举报