LeetCode: Convert Sorted Array to Binary Search Tree && Convert Sorted List to Binary Search Tree

Title:

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

Title:

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

思路:自顶向下

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:

    TreeNode* sortedListToBST(ListNode* head) {
        return maker(head,getLength(head));
    }
    TreeNode* maker(ListNode* head, int len){
        if (len == 0)
            return NULL;
        if (len == 1)
            return new TreeNode(head->val);
        ListNode* cur = getN(head,len/2);
        TreeNode* root = new TreeNode(cur->val);
        root->left = maker(head,len/2);
        root->right = maker(cur->next,len-len/2-1);
    }
    ListNode* getN(ListNode*p, int n){
        ListNode* l = p;
        int count = 0;
        while (count++ < n){
            l = l->next;
        }
        return l;
    }

    int getLength(ListNode* p){
        ListNode * l = p;
        int len = 0;
        while (l){
            len++;
            l = l->next;
        }
        return len;
    }
};

自底向上

class Solution {
public:
TreeNode *sortedListToBST(ListNode *head) {
    int len = 0;
    ListNode *p = head;
    while (p) {
        len++;
        p = p->next;
    }
    return sortedListToBST(head, 0, len - 1);
}
private:
    TreeNode* sortedListToBST(ListNode*& list, int start, int end) {
    if (start > end) return nullptr;
    int mid = start + (end - start) / 2;
    TreeNode *leftChild = sortedListToBST(list, start, mid - 1);
    TreeNode *parent = new TreeNode(list->val);
    parent->left = leftChild;
    list = list->next;
    parent->right = sortedListToBST(list, mid + 1, end);
    return parent;
    }
};
时间: 2024-08-03 08:46:44

LeetCode: Convert Sorted Array to Binary Search Tree && Convert Sorted List to Binary Search Tree的相关文章

leetcode 26. Remove Duplicates from Sorted Array 、80. Remove Duplicates from Sorted Array II

两个题类似,第一个题是不允许有重复的数字,第二个题是允许每个数字最多重复两个,两个题目都要求在原数组上进行操作,并返回生成数组的长度,即空间复杂度为O(1). 两个题都是使用双指针,第一个指针指向生成新的数组的最后一个位置,第二个指针指向当前进行判断的位置. 唯一不同的是, 第二个题需要设置一个变量来控制重复的个数是否超过2 26. Remove Duplicates from Sorted Array class Solution { public: int removeDuplicates(

leetcode 6. 在有序数组旋转后搜索 Search in Rotated Sorted Array

Search in Rotated Sorted Array 难度:Hard Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherw

&lt;LeetCode OJ&gt; Find Minimum in Rotated Sorted Array【153】

153. Find Minimum in Rotated Sorted Array My Submissions Question Total Accepted: 73048 Total Submissions: 209952 Difficulty: Medium Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7

62. Search in Rotated Sorted Array【medium】

62. Search in Rotated Sorted Array[medium] Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, ot

81. Search in Rotated Sorted Array II

Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity? How and why? Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4

26. Remove Duplicates from Sorted Array【easy】

26. Remove Duplicates from Sorted Array[easy] Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with consta

leetCode-数组:Remove Duplicates from Sorted Array

Remove Duplicates from Sorted Array:从排列后的数组中删除重复元素 考察数组的基本操作: class Solution { public int removeDuplicates(int[] nums) { if (nums==null || nums.length==0) return 0; int index = 1; for(int i =1; i<nums.length; i++){ if(nums[i]!=nums[i-1]){ nums[index]

LeetCode OJ - Convert Sorted Array/List to Binary Search Tree

虚函数使用的时机 为什么虚函数不总是适用? 1. 虚函数有事会带来很大的消耗: 2. 虚函数不总是提供所需的行为: 3. 当我们不考虑继承当前类时,不必使用虚函数. 必须使用虚函数的情况: 1. 当你想删除一个表面上指向基类对象,实际却是指向派生类对象的指针,就需要虚析构函数. LeetCode OJ - Convert Sorted Array/List to Binary Search Tree,布布扣,bubuko.com LeetCode OJ - Convert Sorted Arra

leetcode 108 Convert Sorted Array to Binary Search Tree

题目连接 https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/ Convert Sorted Array to Binary Search Tree Description Given an array where elements are sorted in ascending order, convert it to a height balanced BST. /** * Definition f

[leetcode]Convert Sorted Array to Binary Search Tree @ Python

原题地址:http://oj.leetcode.com/problems/convert-sorted-array-to-binary-search-tree/ 题意:将一个排序好的数组转换为一颗二叉查找树,这颗二叉查找树要求是平衡的. 解题思路:由于要求二叉查找树是平衡的.所以我们可以选在数组的中间那个数当树根root,然后这个数左边的数组为左子树,右边的数组为右子树,分别递归产生左右子树就可以了. 代码: # Definition for a binary tree node # class