标签:code val binary return sea tree node for null blog
方法:使用递归的方式
/** * 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 sortedArrayToBST(nums, 0, nums.size()); } typedef vector<int>::iterator Iterator; TreeNode* sortedArrayToBST(vector<int> &nums, int lt, int rt) { if(lt >= rt) return nullptr; int mid = (lt + rt) / 2; TreeNode *ret = new TreeNode(nums[mid]); ret->left = sortedArrayToBST(nums, lt, mid); ret->right = sortedArrayToBST(nums, mid + 1, rt); return ret; } };
Convert Sorted Array to Binary Search Tree
标签:code val binary return sea tree node for null blog
原文地址:http://www.cnblogs.com/chengyuz/p/6724453.html