题目: Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.
关键词:有序单链表、二叉平衡搜索树
二叉树搜索:对于所有节点,顺序是:left children <= current node <= right children;
平衡vs.非平衡:它是一 棵空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树;
解题思路:在链表中,先通过快慢指针将树的根节点找到,返回后再递归构建出二叉平衡搜索树。 由于二叉排序树的中序遍历即有序,也就是与本题中的单链表从头到尾遍历相同,所以可以按照类似中序遍历的做法。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
/**
* Definition for binary tree