来源:Leetcode
原帖:https://oj.leetcode.com/problems/convert-sorted-list-to-binary-search-tree/
题目:
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.
代码:
原帖:https://oj.leetcode.com/problems/convert-sorted-list-to-binary-search-tree/
题目:
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:
int getLength(ListNode *head) {
int length = 0;
while (head) {
length++;
head = head->next;
}
return length;
}
TreeNode *sortedListToBSTHelper(ListNode* &head, int length) {
if (length == 0) return NULL;
TreeNode* left = sortedListToBSTHelper(head, length / 2);
TreeNode* root = new TreeNode(head->val);
head = head->next; // move head pointer!
TreeNode* right = sortedListToBSTHelper(head, length - length / 2 - 1);
root->left = left;
root->right = right;
return root;
}
TreeNode *sortedListToBST(ListNode *head) {
return sortedListToBSTHelper(head, getLength(head));
}
};
No comments:
Post a Comment