[LeetCode 题解]:Convert Sorted List to Binary Search Tree

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

题意:给定一个有序的链表,将其转换成平衡二叉搜索树

思路: 二分法

要构建一个平衡二叉树,二分法无疑是合适的,至于如何分是的代码简洁,就需要用到递归了。

class Solution {
public:
    // find middle element of the list
    ListNode *getmiddleList(ListNode *left,ListNode *right){
        //omit the condition :  left!=right && left->next!=right
        ListNode *pre,*last;
        pre=left; last =left->next;
        while(last!=right){
            last = last->next;
            if(last!=right){
                last = last->next;
                pre=pre->next;
            }
        }
        return pre;
    }

    // retri-BST constructor
    TreeNode *getBST(ListNode *left,ListNode *right){
        TreeNode *root = new TreeNode(0);
        //no leaf
        if(left==right) return NULL;
        // only one leaf
        if(left->next == right){
            root->val=left->val;
            return root;
        }
        //more than one leaf
        ListNode *middle =getmiddleList(left,right);
        root->val = middle->val;
        root->left = getBST(left, middle);
        root->right = getBST(middle->next,right);
        return root;
    }
    TreeNode *sortedListToBST(ListNode *head) {
        TreeNode* root= new TreeNode(0);
        if(head==NULL) return NULL;
        if(head->next==NULL){
            root->val=head->val;
            root->left=root->right=NULL;
            return root;
        }
        ListNode *left,*middle,*right;
        middle=left=head;
        right=head->next;
        while(right){
            right=right->next;
            if(right){
                right=right->next;
                middle=middle->next;
            }
        }
        root->val=middle->val;
        root->left = getBST(left, middle);
        root->right= getBST(middle->next,right);
        return root;
    }
};     

转载请注明出处:http://www.cnblogs.com/double-win/ 谢谢!

[LeetCode 题解]:Convert Sorted List to Binary Search Tree

时间: 2024-10-12 12:55:29

[LeetCode 题解]:Convert Sorted List to Binary Search Tree的相关文章

leetcode -day19 Convert Sorted List to Binary Search Tree

1.  Convert Sorted List to Binary Search Tree Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. 分析:将一个升序排列的链表转换为平衡二叉搜索树,采用递归的方式,先找到链表的中点,作为二叉树的根,然后递归求解左右子树. 如下: class Solution { public: Tr

leetcode No109. Convert Sorted List to Binary Search Tree

Question: Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. 把有序链表转化成平衡的BST Algorithm: 把链表转化成数组,再根据leetcode No108. Convert Sorted Array to Binary Search Tree的方法 找到数组中间的元素,作为根节点,则根节点左边是左子树,根节点

[Leetcode][BST][Convert Sorted Array to Binary Search Tree]

把一个排好序的vector转换为一颗二分查找树. 很简单的题目,递归即可,保证边界不要出错. 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 1

leetcode 109 Convert Sorted List to Binary Search Tree

题目连接 https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/ Convert Sorted List to Binary Search Tree Description Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. /** * De

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 dfs Convert Sorted List to Binary Search Tree

Convert Sorted List to Binary Search Tree Total Accepted: 21420 Total Submissions: 78476My Submissions Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. 题意:把有序单链表转换为二叉查找树 思路: 用链表中心作为当作二叉树的根,

[Leetcode][JAVA] Convert Sorted Array to Binary Search Tree && Convert Sorted List to Binary Search Tree

Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 很简单的二分法,只要给出Array的开始和结束下标作为参数传入即可. 1 public TreeNode sortedArrayToBST(int[] num) { 2 return constructBST(num,

leetcode 【 Convert Sorted List to Binary Search Tree 】python 实现

题目: Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. 代码:oj测试通过 Runtime: 178 ms 1 # Definition for a binary tree node 2 # class TreeNode: 3 # def __init__(self, x): 4 # self.val = x 5 # self

LeetCode 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. 题目标签:Tree 这道题目给了我们一个有序数组,从小到大.让我们把这个数组转化为height balanced BST. 首先来看一下什么是binary search tree: 每一个点的left < 节点 < right, 换一句话说,每一个点的值要大于左边的,小于右边的. 那么什么是heigh