Leetcode 108. Convert Sorted Array to Binary Search Tree
顺序数组建树,要从中间开始建树。 /** * 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) { return buildTree(nums, 0, nums.size() - 1); } TreeNode* buildTree(vector<int>& nums, int l, int r){ if(l > r) return NULL; int m = l + ((r - l) >> 1); TreeNode *t = new TreeNode(nums[m]); t->left = buildTree(nums, l, m - 1); t->right = buildTree(nums, m + 1, r); return t; } }; 来源: https://www.cnblogs.com/littlepage/p/12271803.html