标签:
/** * 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 *createTree(vector<int> &nums, int left, int right) { if(left > right) return NULL; int mid = (left + right)/2; TreeNode *root = new TreeNode(nums[mid]); root->left = createTree(nums, left, mid-1); root->right = createTree(nums, mid+1, right); return root; } TreeNode *sortedArrayToBST(vector<int> &nums) { return createTree(nums, 0, nums.size()-1); } };
[leetcode] Convert Sorted Array to Binary Search Tree
标签:
原文地址:http://www.cnblogs.com/lxd2502/p/4484923.html