-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path109.cpp
46 lines (43 loc) · 1.03 KB
/
109.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//
// 109.cpp
// leetcode
//
// Created by R Z on 2018/8/16.
// Copyright © 2018年 R Z. All rights reserved.
//
#include <stdio.h>
/**
* Definition for singly-linked list.*/
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
/**
* Definition for a binary tree node.*/
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
public:
TreeNode* sortedListToBST(ListNode* head) {
if(head==NULL) return NULL;
return toBST(head,NULL);
}
TreeNode* toBST(ListNode* head, ListNode* tail){
ListNode *slow=head;
ListNode *fast=head;
if(head==tail) return NULL;
while(fast!=tail && fast->next!=tail){
slow=slow->next;
fast=fast->next->next;
}
TreeNode* thead = new TreeNode(slow->val);
thead->left=toBST(head,slow);
thead->right=toBST(slow->next,tail);
return thead;
}
};