摘要:
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.Top down 的解题方法:1. 将LinkedList的值保存到一个数组中,转化成Convert Sorted Array to Binary Search Tree 来解决时间复杂度为O(n), 空间复杂度为O(n) 1 /** 2 * Definition for singly-linked list. 3 * public class ListNode { 4 *... 阅读全文
摘要:
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.[解题思路]递归确定每棵树的根节点的值,这里根节点的值是二分搜索的中值由于这里是有序数组,确定root的时间复杂度为O(1), 整个算法的时间复杂度为O(n),n为节点数目。 1 /** 2 * Definition for binary tree 3 * public class TreeNode { 4 * int val; 5 * TreeNode left; 6 * ... 阅读全文