108. Convert Sorted Array to Binary Search Tree
problem description
Given an array 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:
algorithm thought
这里需要生成一个平衡BST。平衡BST有很多种表示方法,但是我们只需要生成一种就行,那当然是生成最简单的bst。每次到一个节点时,找到中点,将一个有序数组对分。直到叶节点。这样就能保证最后得到的是平衡二叉树
code
algorithm analysis
时间复杂度应该是O(n)遍历到所有节点,最后生成一个树
Last updated