108. Convert Sorted Array to Binary Search Tree


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

方法1: Recursion

官方题解:https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/solution/
思路:

找到midpoint, new 出一个新的节点,每次recursively build up left and right tree, connect to root。

Complexity

Time complexity: O(n log n)
Space complexity: Space Complexity: O(logN). Since we are resorting to recursion, there is always the added space complexity of the recursion stack that comes into picture. This could have been: O(N) for a skewed tree, but the question clearly states that we need to maintain the height balanced property. This ensures the height of the tree to be bounded by O(logN). Hence, the space complexity is O(logN).

易错点:

  1. 用head传递recursion需要在edge case中需要处理head只有一个节点的情况:如果不终止递归,会反复创建head这个节点(因为slow不会移动),造成overflow。
  2. 递归本身的space complexity也要计算在内。
class Solution {
public:
    TreeNode* sortedArrayToBST(vector<int>& nums) {
        return toBST(nums, 0, nums.size() - 1);
    }
    
    TreeNode* toBST(vector<int> & nums, int left, int right){
        if (left > right) return nullptr;
        
        int mid = left + (right - left) / 2;
        TreeNode* cur = new TreeNode(nums[mid]);
        cur -> left = toBST(nums, left, mid - 1);
        cur -> right = toBST(nums, mid + 1, right);
        return cur;
    }
};

另外还有不用断开原链表的做法:
grandyang: http://www.cnblogs.com/grandyang/p/4295618.html。传递时同时input head and tail。注意此时导致他们相等的那个function是helper(head, head),而这个时候slow = head已经创建过一个节点了,故递归终止返回null就够了。

class Solution {
public:
    TreeNode* sortedListToBST(ListNode* head) {
        if (!head) return NULL;
        return helper(head, NULL);
    }
    TreeNode* helper(ListNode* head, ListNode* tail) {
        if (head == tail) return NULL;
        ListNode *slow = head, *fast = head;
        while (fast != tail && fast->next != tail) {
            slow = slow->next;
            fast = fast->next->next;
        }
        TreeNode *cur = new TreeNode(slow->val);
        cur->left = helper(head, slow);
        cur->right = helper(slow->next, tail);
        return cur;
    }
};

方法2: Recursion + Conversion to Array

思路:

his approach is a classic example of the time-space tradeoff.

You can get the time complexity down by using more space.

Complexity

Time Complexity: The time complexity comes down to just O(N) now since we convert the linked list to an array initially and then we convert the array into a BST. Accessing the middle element now takes O(1) time and hence the time complexity comes down.
Space Complexity: Since we used extra space to bring down the time complexity, the space complexity now goes up to O(N) as opposed to just O(logN) in the previous solution. This is due to the array we construct initially.

方法3:

官解3.

思路:

Time Complexity: The time complexity is still O(N) since we still have to process each of the nodes in the linked list once and form corresponding BST nodes.
Space Complexity: O(logN) since now the only extra space is used by the recursion stack and since we are building a height balanced BST, the height is bounded by \log NlogN.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值