排好序的...
中间是root , root左边是left,root右边是right
递归建树.
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: TreeNode* createTree(vector<int>&num , int start , int end){ if(end < start) return nullptr; int mid = (start + end) / 2; TreeNode* root = new TreeNode(num[mid]); root -> left = createTree(num , start , mid - 1); root -> right = createTree(num , mid + 1 , end); return root; } TreeNode *sortedArrayToBST(vector<int> &num) { int size = num.size(); if (size == 0) return nullptr; return createTree(num , 0 , size - 1); } };