108. Convert Sorted Array to Binary Search [Python]

108. Convert Sorted Array to Binary Search

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.

给定一个升序的矩阵,将它转换成一棵平衡二叉树。

其中,平衡二叉树是指树中每个节点子树高度差的绝对值不超过1的树。

思路:由于左右高度差的绝对值不能超过1,所以树根节点应选择升序列表中间的元素,即nums[len(nums)//2]。

该元素前面的元素(比根节点小的元素)同理递归地放入左子树;后面的元素递归地放入右子树。

最后返回各个节点。

代码

class Solution:
    def sortedArrayToBST(self, nums):
        """
        @param type nums: List[int]
        @param rtype: TreeNode
        """
        if nums == None:
            return
        if len(nums) == 1:
            return nums[0]
        root = nums[len(nums)//2]
        self.sortedArrayToBST(nums[:len(nums)//2])
        self.sortedArrayToBST(nums[len(nums)//2+1:])
        return root

原文地址:https://www.cnblogs.com/wyz-2020/p/12386366.html

时间: 2024-10-09 01:44:57

108. Convert Sorted Array to Binary Search [Python]的相关文章

LeetCode OJ 108. Convert Sorted Array to Binary Search Tree DFS求解

题目链接:https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/ 108. Convert Sorted Array to Binary Search Tree My Submissions Question Total Accepted: 68378 Total Submissions: 187560 Difficulty: Medium Given an array where elements ar

leetcode 108 Convert Sorted Array to Binary Search Tree

题目连接 https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/ Convert Sorted Array to Binary Search Tree Description Given an array where elements are sorted in ascending order, convert it to a height balanced BST. /** * Definition f

LeetCode 108. Convert Sorted Array to Binary Search Tree (有序数组转化为二叉搜索树)

Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 题目标签:Tree 这道题目给了我们一个有序数组,从小到大.让我们把这个数组转化为height balanced BST. 首先来看一下什么是binary search tree: 每一个点的left < 节点 < right, 换一句话说,每一个点的值要大于左边的,小于右边的. 那么什么是heigh

LC.108.Convert Sorted Array to Binary Search Tree

https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/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 bin

【leetcode】108. Convert Sorted Array to Binary Search Tree

题目如下: 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 m

[leedcode 108] Convert Sorted Array to Binary Search Tree

Given an array where elements are sorted in ascending order, convert it to a height balanced BST. /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ p

108.Convert Sorted Array to Binary Search Tree

Given an array where elements are sorted in ascending order, convert it toa height balanced BST. HideTags Tree  Depth-first Search #pragma once #include<iostream> #include<vector> using namespace std; struct TreeNode { int val; TreeNode *left;

leetcode 108 Convert Sorted Array to Binary Search Tree ----- java

Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 给一个排好序的数组,然后求搜索二叉树 其实就是二分法,不难. /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNo

Java for LeetCode 108 Convert Sorted Array to Binary Search Tree

Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 解题思路: 首先要理解,什么叫做height balanced BST Java for LeetCode 110 Balanced Binary Tree,然后就十分容易了,JAVA实现如下: public TreeNode sortedArrayToBST(int[] nums) { return