Problem
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.
For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.
Example:
1 |
|
Explanation
- Similar to 108. Convert Sorted Array to Binary Search Tree, we can find out the middle element of this linkedlist by using left and fast pointer. Since we don’t know the tail node, so we pass it as null.
Solution
1 |
|