Leetcode: Convert Sorted List to Binary Search Tree
2014-01-04 11:37
344 查看
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.
本以为时间复杂度过不了,没想到竟然过了。
======================第二次====================
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
/**
* 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 *sortedListToBST(ListNode *head) {
if (head == NULL) {
return NULL;
}
else if (head->next == NULL) {
return new TreeNode(head->val);
}
ListNode *fast = head;
ListNode *slow = head;
ListNode *prev = NULL;
while (fast != NULL && fast->next != NULL) {
fast = fast->next->next;
prev = slow;
slow = slow->next;
}
prev->next = NULL;
TreeNode *root = new TreeNode(slow->val);
root->left = sortedListToBST(head);
root->right = sortedListToBST(slow->next);
return root;
}
};
本以为时间复杂度过不了,没想到竟然过了。
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ /** * 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 *sortedListToBST(ListNode *head) { return listToBSTUtil(head, NULL); } TreeNode *listToBSTUtil(ListNode *begin, ListNode *end) { if (begin == NULL || begin == end) { return NULL; } if (begin->next == end) { return new TreeNode(begin->val); } ListNode *mid = begin; ListNode *cur = begin; while (cur != end && cur->next != end) { mid = mid->next; cur = cur->next->next; } TreeNode *root = new TreeNode(mid->val); root->left = listToBSTUtil(begin, mid); root->right = listToBSTUtil(mid->next, end); return root; } };
======================第二次====================
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
/**
* 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 *sortedListToBST(ListNode *head) {
if (head == NULL) {
return NULL;
}
else if (head->next == NULL) {
return new TreeNode(head->val);
}
ListNode *fast = head;
ListNode *slow = head;
ListNode *prev = NULL;
while (fast != NULL && fast->next != NULL) {
fast = fast->next->next;
prev = slow;
slow = slow->next;
}
prev->next = NULL;
TreeNode *root = new TreeNode(slow->val);
root->left = sortedListToBST(head);
root->right = sortedListToBST(slow->next);
return root;
}
};
相关文章推荐
- Convert Sorted List to Binary Search Tree - LeetCode 109
- LeetCode Convert Sorted List to Binary Search Tree
- LeetCode(109) Convert Sorted List to Binary Search Tree
- [LeetCode] Convert Sorted List to Binary Search Tree
- LeetCode Convert Sorted List to Binary Search Tree
- LeetCode - Convert Sorted List to Binary Search Tree
- Java for LeetCode 109 Convert Sorted List to Binary Search Tree
- [leetcode] Convert Sorted List to Binary Search Tree
- Leetcode Convert Sorted List to Binary Search Tree
- LeetCode: Convert Sorted List to Binary Search Tree
- LeetCode :: Convert Sorted Array (link list) to Binary Search Tree [tree]
- leetCode 108,109:Convert Sorted Array/List to Binary Search Tree
- LeetCodeConvert Sorted List to Binary Search Tree
- [LeetCode]Convert Sorted List to Binary Search Tree
- leetcode 109 Convert Sorted List to Binary Search Tree
- LeetCode Convert Sorted List to Binary Search Tree
- 5 Convert Sorted List to Binary Search Tree_Leetcode
- [leetcode]convert-sorted-list-to-binary-search-tree
- leetcode---Convert Sorted List to Binary Search Tree---二叉搜索树
- [LeetCode]Convert Sorted List to Binary Search Tree