Source of the topic:
https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/
Test Instructions Analysis:
A well-ordered array is given, which forms a highly balanced search binary tree based on this data.
Topic Ideas:
The median is the root node, the median number is the left dial hand tree, and the right subtree is the left.
Code (Python):
#Definition for a binary tree node.#class TreeNode (object):#def __init__ (self, x):#self.val = x#self.left = None#self.right = Noneclasssolution (object):defSortedarraytobst (Self, nums):""": Type Nums:list[int]: Rtype:treenode"""size=Len (nums)ifSize = =0:returnNoneifsize = = 1: returnTreeNode (nums[0]) size= 2Root=TreeNode (nums[size]) Root.left=Self.sortedarraytobst (nums[:size]) root.right= Self.sortedarraytobst (nums[size + 1:]) returnRoot
View Code
[Leetcode] (python): 108-convert Sorted Array to Binary Search Tree