LeetCode #109 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.

 

分析:

给定一个有序链表,将其转换为一个高度平衡的二叉搜索树。

首先遍历链表获得链表长度。之后通过递归生成二叉搜索树。其生成过程和树的中序遍历类型:先是左子树,然后是根节点,最后是右子树。每添加一个树节点,就把链表节点的值赋给树节点,然后指针指向下一个链表节点。

Continue reading LeetCode #109 Convert Sorted List to Binary Search Tree (有序链表转换为平衡二叉搜索树)