标签:else ini root cto sort return amp link bin
1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * }; 8 */ 9 /** 10 * Definition for a binary tree node. 11 * struct TreeNode { 12 * int val; 13 * TreeNode *left; 14 * TreeNode *right; 15 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 16 * }; 17 */ 18 class Solution 19 { 20 public: 21 TreeNode* sortedListToBST(ListNode* head) 22 { 23 vector<int> nums; 24 while(head) 25 { 26 nums.push_back(head->val); 27 head = head->next; 28 } 29 30 if(nums.size() == 0) return NULL; 31 int l = 0,r = nums.size() - 1; 32 return slove(nums,l,r); 33 } 34 35 TreeNode* slove(vector<int>& nums,int l,int r) 36 { 37 int mid = (l + r)/2; 38 TreeNode* root = new TreeNode(nums[mid]); 39 if(mid != l) root->left = slove(nums,l,mid - 1); 40 else root->left = 0; 41 42 if(mid != r) root->right = slove(nums,mid + 1,r); 43 else root->right = 0; 44 45 return root; 46 } 47 };
标签:else ini root cto sort return amp link bin
原文地址:https://www.cnblogs.com/yuhong1103/p/12612317.html