Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.
Ideas:
First of all, we need to know what is BST. it is a binary tree, with a node, the value of this node is bigger than all left sub-tree node value, while smaller than all node in the right-subtree. While a hight balanced BST means the hight of the left tree is the equal to the right tree.
So, the ideas is as followings:
1. get the number of list node in linkedlist.
2. The middle one is the root. while the first half part of node is the left BST, the right half part of node put into the right BST.
<!-----------------Accepted----------------------->
/**
* 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;
if(head->next == NULL) return new TreeNode(head->val);
ListNode *dummy = new ListNode(0);
dummy->next = head;
ListNode
*pSlow = head,
*pLast = dummy,
*pFast = head;
while(pFast != NULL && pFast->next!= NULL)
{
pLast = pSlow;
pSlow = pSlow->next;
pFast = pFast->next->next;
}
pLast->next = NULL;
TreeNode *root = new TreeNode(pSlow->val);
ListNode *rightList = pSlow->next;
ListNode *leftList = dummy->next;
root->left = sortedListToBST(leftList);
root->right = sortedListToBST(rightList);
delete dummy;
return root;
}
};
<----------------------------Accepted------------------>/**
* 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);
//use slow and fast pointer to find the middle node in list
ListNode
*pre = head,
*slow = head,
*fast = head;
while(fast!=NULL && fast->next!=NULL)
{
pre = slow;
slow = slow->next;
fast = fast->next->next;
}
pre->next = NULL;
//create a treenode
TreeNode *headTree = new TreeNode(slow->val);
headTree->left = sortedListToBST(head);
headTree->right = sortedListToBST(slow->next);
return headTree;
}
};
/**
* 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 head; <span style="color:#ff0000;">//BUG1: head should change to NULL</span>
else if(head->next == null) return new TreeNode(head->val); <span style="color:#ff0000;">//BUG2: null should be NULL or nullptr</span>
//use slow and fast pointer to find the middle node in list
ListNode
*pre = head,
*slow = head,
*fast = head;
while(fast->next) <span style="color:#ff0000;">//BUG3: should be while(fast!=NULL && fast->next!=NULL)</span>
{
pre = slow;
slow = slow->next;
fast = fast->next->next;
}
pre->next = NULL;
//create a treenode
TreeNode *headTree = new TreeNode(slow->val);
headTree->left = sortedListToBST(head);
headTree->right = sortedListToBST(slow->next);
return headTree;
}
};