题目
原题页面:https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/
题目类型:Tree, Depth-first Search
难度评价:Medium
类似题目:(M) Convert Sorted List to Binary Search Tree
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.
分析
用递归的方法,每次选择当前子序列的中点作为子树的root,然后再递归对左边子序列和右边子序列进行处理。
原题中标注为深度搜索DFS。这里是构建二叉树,构建的顺序类似于二叉树的先序遍历。由于树是一种特殊的图,因此二叉树的先序遍历其实就是图的DFS,而层次遍历就是图的BFS。
代码
1 | class Solution: |
本文地址:https://xnerv.wang/leetcode-convert-sorted-array-to-binary-search-tree/
