标签:turn nbsp bst node 二叉搜索树 style tor treenode return
1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution 11 { 12 public: 13 TreeNode* sortedArrayToBST(vector<int>& nums) 14 { 15 if(nums.size() == 0) return NULL; 16 int l = 0,r = nums.size() - 1; 17 return slove(nums,l,r); 18 } 19 20 TreeNode* slove(vector<int>& nums,int l,int r) 21 { 22 int mid = (l + r)/2; 23 TreeNode* root = new TreeNode(nums[mid]); 24 if(mid != l) root->left = slove(nums,l,mid - 1); 25 else root->left = 0; 26 27 if(mid != r) root->right = slove(nums,mid + 1,r); 28 else root->right = 0; 29 30 return root; 31 } 32 };
标签:turn nbsp bst node 二叉搜索树 style tor treenode return
原文地址:https://www.cnblogs.com/yuhong1103/p/12612297.html