Leetcode -- Python -- Convert Sorted Array to Binary Search Tree
2014-06-06 03:42
543 查看
Leetcode题目:
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.
分析:
BST-- binary search tree-- 二分查找树,就是数的三个元素(root, left, right)的值的大小顺序服从 -- left < root < right. height balanced的意思是两个分支的深度差不超过一。由于这是用一个已经是升序排列的数列作为输入,那么我们通过divide
and conquer, 左半作为左支,右半作为右支,中数作为root.两支平均可以保证height balanced.
Python程序:
# Definition for a binary tree node
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
# @param num, a list of integers
# @return a tree node
def sortedArrayToBST(self, num):
if num:
root = TreeNode(0)
num_l = len(num)
root.val = num[num_l/2]
if num_l/2-1 >= 0:
root.left = self.sortedArrayToBST(num[0:num_l/2])
if num_l/2+1 < num_l :
root.right = self.sortedArrayToBST(num[num_l/2 +1:])
return root
else:
return None
问题:
1. 为什么是num[0:num_l/2]?
Python中在获取一个list的一部分时,如果是a = [1,2,3], 注意,a[0,0] = [], a[0,1] = [1], a[0,2] =[1,2]
2.为什么 是num_l/2 - 1 >=0 ,而num_l/2 + 1 < num_l? 一个有等号,一个没有等号。
这都是为了使得,不要超限(num[num_l] 是非法的。
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.
分析:
BST-- binary search tree-- 二分查找树,就是数的三个元素(root, left, right)的值的大小顺序服从 -- left < root < right. height balanced的意思是两个分支的深度差不超过一。由于这是用一个已经是升序排列的数列作为输入,那么我们通过divide
and conquer, 左半作为左支,右半作为右支,中数作为root.两支平均可以保证height balanced.
Python程序:
# Definition for a binary tree node
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
# @param num, a list of integers
# @return a tree node
def sortedArrayToBST(self, num):
if num:
root = TreeNode(0)
num_l = len(num)
root.val = num[num_l/2]
if num_l/2-1 >= 0:
root.left = self.sortedArrayToBST(num[0:num_l/2])
if num_l/2+1 < num_l :
root.right = self.sortedArrayToBST(num[num_l/2 +1:])
return root
else:
return None
问题:
1. 为什么是num[0:num_l/2]?
Python中在获取一个list的一部分时,如果是a = [1,2,3], 注意,a[0,0] = [], a[0,1] = [1], a[0,2] =[1,2]
2.为什么 是num_l/2 - 1 >=0 ,而num_l/2 + 1 < num_l? 一个有等号,一个没有等号。
这都是为了使得,不要超限(num[num_l] 是非法的。
相关文章推荐
- 【LeetCode with Python】 Convert Sorted Array to Binary Search Tree
- 【Leetcode】【python】Convert Sorted Array to Binary Search Tree
- [LeetCode]题解(python):108-Convert Sorted Array to Binary Search Tree
- [leetcode]Convert Sorted Array to Binary Search Tree @ Python
- LeetCode-Convert Sorted Array to Binary Search Tree
- leetcode 11: Convert Sorted Array to Binary Search Tree
- leetcode -- Convert Sorted Array to Binary Search Tree
- LeetCode - Convert Sorted Array to Binary Search Tree
- [LeetCode] Convert Sorted Array to Binary Search Tree, Solution
- [LeetCode]Convert Sorted Array to Binary Search Tree
- LeetCode: Convert Sorted Array to Binary Search Tree
- Leetcode 108 Convert Sorted Array to Binary Search Tree
- LeetCode Convert Sorted Array to Binary Search Tree
- LeetCode: Convert Sorted Array to Binary Search Tree
- LeetCode_Convert Sorted Array to Binary Search Tree
- [Leetcode] Convert Sorted Array to Binary Search Tree
- [leetcode]Convert Sorted Array to Binary Search Tree
- leetcode 11: Convert Sorted Array to Binary Search Tree
- leetcode: Convert Sorted Array to Binary Search Tree
- Leetcode 108 Convert Sorted Array to Binary Search Tree