[Leetcode problem]: Convert sorted list to Binary Search Tree

Source: Internet
Author: User

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

Give an ordered linked list and convert it into a balanced binary search tree.

Idea: bipartite

To build a balanced binary tree, the bipartite method is undoubtedly appropriate. As for how to separate the Code with conciseness, recursion is required.

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;    }};     

Reprint please indicate the source: http://www.cnblogs.com/double-win/ thank you!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.