简单的递归题
/** * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */ class Solution { public: /** * * @param num int整型vector * @return TreeNode类 */ TreeNode* sortedArrayToBST(vector<int>& num) { // write code here return dfs(num, 0, num.size() - 1); } TreeNode* dfs(vector<int> & num, int l, int r) { if(l > r) return nullptr; if(l == r) return new TreeNode(num[l]); auto root = new TreeNode(num[(l + r + 1) >> 1]); root->left = dfs(num, l, ((l + r + 1) >> 1) - 1); root->right = dfs(num, ((l + r + 1) >> 1) + 1, r); return root; } };