leetcode 108 Convert Sorted Array to Binary Search Tree
给定一个有序的数组,生成平衡二叉树。
TreeNode* sortedArrayToBST(vector<int>& nums) { if (nums.size() == 0) return NULL; return sort(nums, 0, nums.size() - 1); } TreeNode* sort(vector<int>& nums, int start, int end) { if (start > end) return NULL; int mid = (start + end) / 2; TreeNode* r = new TreeNode(nums[mid]); r->left = sort(nums, start, mid - 1); r->right = sort(nums, mid + 1, end); return r; }
【本文章出自博客园willaty,转载请注明作者出处,误差欢迎指出~】