博文中会简要介绍Leetcode P0108题目分析及解题思路。
“Convert Sorted Array to Binary Search Tree”是比较简单和基础的树的题目,运用了深度优先搜索,这里是递归回溯的思路。这道题需要我们构造一个平衡二叉搜索树,我们可以每次将整个数组二分,其中点作为BST的根结点,然后运用递归回溯的思路,向下层生长。
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.
以下是Java的题解代码实现。
以下是C++的题解代码实现。