点击打开链接
//典型自上而下 递归使用
//只要构造出一种情况,因此不需要回溯
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <cstring>
#include <string.h>
#include <algorithm>
#include <vector>
#include <numeric>
#include <limits>
#include <math.h>
#include <queue>
using namespace std;
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
public:
TreeNode *sortedArrayToBST(vector<int>& num, int l, int r)
{
if(l>r) return NULL;
int m=(l+r)/2;
TreeNode *root=new TreeNode(num[m]);//中间节点赋值了
root->left = sortedArrayToBST(num, l, m-1);
root->right = sortedArrayToBST(num, m+1, r);
return root;
}
TreeNode* sortedListToBST(ListNode* head) {
vector<int> vi;
for(ListNode* p=head; p!=NULL;p=p->next){
vi.push_back(p->val);
}
return sortedArrayToBST(vi, 0, vi.size()-1);//起始点。终止点
}
};
int main()
{
return 0;
}