34. Convert sorted list to Binary Search Tree & convert sorted array to Binary Search Tree

Source: Internet
Author: User
Convert sorted list to Binary Search Tree

OJ: https://oj.leetcode.com/problems/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.

Idea: Use the intermediate point as the root node and create it in the FIFO order.

/** * Definition for singly-linked list. * struct ListNode { *     int val; *     ListNode *next; *     ListNode(int x) : val(x), next(NULL) {} * }; *//** * Definition for binary tree * struct TreeNode { *     int val; *     TreeNode *left; *     TreeNode *right; *     TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ListNode* findMiddleNode(ListNode *head, int length) {    if(length <= 0) return NULL;    int mid = (length+1) / 2;    for(int i = 1; i < mid; ++i) head = head->next;    return head;}TreeNode* createBST(ListNode *head, int length) {    ListNode *pMid = findMiddleNode(head, length);    TreeNode *root = NULL;    if(pMid) {        root = new TreeNode(pMid->val);         root->left = createBST(head, (length-1) >> 1);        root->right = createBST(pMid->next, length / 2);    }    return root;}int getLength(ListNode *head) {    int len = 0;    while(head) { len++; head = head->next; }    return len;}class Solution {public:    TreeNode *sortedListToBST(ListNode *head) {        return createBST(head, getLength(head));    }};

 

Convert sorted array to Binary Search Tree

 

OJ: https://oj.leetcode.com/problems/convert-sorted-array-to-binary-search-tree/

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

Same as above.

/** * Definition for binary tree * struct TreeNode { *     int val; *     TreeNode *left; *     TreeNode *right; *     TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */TreeNode* createBST(vector<int> &num, int l, int h) {    if(l > h) return NULL;    int mid = (l+h)/2;    TreeNode *root = new TreeNode(num[mid]);    root->left = createBST(num, l, mid-1);    root->right = createBST(num, mid+1, h);    return root;}class Solution {public:    TreeNode *sortedArrayToBST(vector<int> &num) {        return createBST(num, 0, num.size()-1);    }};

 

34. Convert sorted list to Binary Search Tree & convert sorted array to Binary Search Tree

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.