Q109. Convert Sorted List to Binary Search Tree
直达:https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/description/
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 ofeverynode never differ by more than 1.
Example:
Given the sorted linked list: [-10,-3,0,5,9],
One possible answer is: [0,-3,9,-10,null,5], which represents the following height balanced BST:
0
/ \
-3 9
/ /
-10 5分析
和Q108一样,重点是如何确定链表中间元素以及如何切割链表。
用两个指针遍历,快指针fast每次走两步,慢指针slow每次走一步,当快指针走到结尾处(或者倒数第二个)尾指针正好走到中间(或者左偏一格),则slow的下一个便是中间指针,如下图。
slow fast
| |
-10, -3, 0, 5, 9
| |
head mid将mid作为根节点,head到slow作为左子树链表,mid->next到最后作为右子树的链表,递归进行直到链表为空或者只剩一个元素的时候返回即可。
C++代码
Last updated
Was this helpful?