Topic Connection
https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/
Convert Sorted Array to Binary Search treedescription
Given an array where elements is sorted in ascending order, convert it to a height balanced BST.
/** * 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* sortedarraytobst (vector<int>& nums) { int n = 0; if (! ( n = nums.size ())) return nullptr; Nums.insert (Nums.begin (), 0); function<treenode* (int, int) > built = [&] (int l, int r)->treenode* { if (L > R) return nullptr; int m = (L + r) >> 1; TreeNode *x = new TreeNode (nums[m]); X->left = built (l, m-1); X->right = built (M + 1, R); return x; }; return built (1, n); };
Leetcode 108 Convert Sorted Array to Binary Search Tree