[C + +] leetcode:100 Convert Sorted Array to Binary Search tree (AVL tree)

Source: Internet
Author: User

Title:Given An array where elements is sorted in ascending order, convert it to a height balanced BST.

idea: give a sorted array, how to construct a balanced binary search tree? Balanced binary search tree requires that the height difference of the left and right subtree of any node cannot exceed one, also called a height-balanced tree. If we were to choose an element from a sorted array to do the root of the tree, which tree would we choose? Intuitively, we feel that we should choose the median of the array to do the root, and the fact is that the root of the balanced binary lookup tree (AVL tree) should be the intermediate element of the sorted array. ( we take the middle node of the sorted array as the root, which guarantees that the number of elements in the left and right subtree is not more than 1, then it is definitely a balanced binary tree.) But not the only AVL tree, but in the project this selection of intermediate nodes to do root of the search tree is very common, convenient two-point search. we use this element to create the root node of the tree, next to the left and right of the two parts of the array, continue to select the middle element, recursive, Zuozi root should be the left part of the middle element of the array, the right sub-tree root is also the middle element of the part of the array. The method of divide and conquer is used to solve the sub-problem continuously.

Attention:

1. Recursive termination condition, Start > end, return null. It also contains the case where the array is empty (start = 0, end =-1).

AC Code:

/** * Definition for binary tree * struct TreeNode {*     int val; *     TreeNode *left; *     TreeNode *right; *     Tre Enode (int x): Val (x), left (null), right (NULL) {} *}; */class Solution {public:    TreeNode *sortedarraytobst (vector<int> &num) {        return sortedarraytobst_ Helper (num, 0, Num.size ()-1);    } Private:    treenode* sortedarraytobst_helper (vector<int> &num, int start, int end)    {        if (start > End)            return NULL;        int mid = start + (End-start)/2;        treenode* root = new TreeNode (Num[mid]);        Root->left = Sortedarraytobst_helper (num, start, mid-1);        Root->right = Sortedarraytobst_helper (num, mid+1, end);        return root;    }};




[C + +] leetcode:100 Convert Sorted Array to Binary Search tree (AVL tree)

Related Article

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.