原题地址:http://oj.leetcode.com/problems/convert-sorted-array-to-binary-search-tree/

题意:将一个排序好的数组转换为一颗二叉查找树,这颗二叉查找树要求是平衡的。

解题思路:由于要求二叉查找树是平衡的。所以我们可以选在数组的中间那个数当树根root,然后这个数左边的数组为左子树,右边的数组为右子树,分别递归产生左右子树就可以了。

代码:

# 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):
length = len(num)
if length == 0:
return None
if length == 1:
return TreeNode(num[0])
root = TreeNode(num[length / 2])
root.left = self.sortedArrayToBST(num[:length/2])
root.right = self.sortedArrayToBST(num[length/2 + 1:])
return root

[leetcode]Convert Sorted Array to Binary Search Tree @ Python的更多相关文章

  1. LeetCode:Convert Sorted Array to Binary Search Tree,Convert Sorted List to Binary Search Tree

    LeetCode:Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in asce ...

  2. LeetCode: Convert Sorted Array to Binary Search Tree 解题报告

    Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascending ord ...

  3. [LeetCode] Convert Sorted Array to Binary Search Tree 将有序数组转为二叉搜索树

    Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 这道 ...

  4. LeetCode——Convert Sorted Array to Binary Search Tree

    Description: Given an array where elements are sorted in ascending order, convert it to a height bal ...

  5. LeetCode - Convert Sorted Array to Binary Search Tree

    题目: Given an array where elements are sorted in ascending order, convert it to a height balanced BST ...

  6. LeetCode Convert Sorted Array to Binary Search Tree(数据结构)

    题意: 将一个有序的数组建成一棵平衡的BST树. 思路: 因为数组已经有序,每次可以从中点开始建根,再递归下去分别处理左/右子树. /** * Definition for a binary tree ...

  7. [leetcode]Convert Sorted List to Binary Search Tree @ Python

    原题地址:http://oj.leetcode.com/problems/convert-sorted-list-to-binary-search-tree/ 题意:将一条排序好的链表转换为二叉查找树 ...

  8. 【LeetCode OJ】Convert Sorted Array to Binary Search Tree

    Problem Link: http://oj.leetcode.com/problems/convert-sorted-array-to-binary-search-tree/ Same idea ...

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

    108. 将有序数组转换为二叉搜索树 108. Convert Sorted Array to Binary Search Tree 题目描述 将一个按照升序排列的有序数组,转换为一棵高度平衡二叉搜索 ...

随机推荐

  1. CPU线程 和 Java线程

    一 cpu个数.核数.线程数的关系 cpu个数:是指物理上,也及硬件上的核心数: 核数:是逻辑上的,简单理解为逻辑上模拟出的核心数:一个CPU核心数模拟出2线程的CPU 线程数:是同一时刻设备能并行执 ...

  2. python3.6 利用requests和正则表达式爬取猫眼电影TOP100

    import requests from requests.exceptions import RequestException from multiprocessing import Pool im ...

  3. 1002 A+B for Polynomials (25)(25 point(s))

    problem 1002 A+B for Polynomials (25)(25 point(s)) This time, you are supposed to find A+B where A a ...

  4. python opencv3 grabcut前景检测

    git:https://github.com/linyi0604/Computer-Vision import numpy as np import cv2 import matplotlib.pyp ...

  5. JS (function (window, document, undefined) {})(window, document)的真正含义

    原文地址:What (function (window, document, undefined) {})(window, document); really means 按原文翻译 在这篇文章中,我 ...

  6. BZOJ 5059: 前鬼后鬼的守护 可并堆 左偏树 数学

    https://www.lydsy.com/JudgeOnline/problem.php?id=5059 题意:将原序列{ai}改为一个递增序列{ai1}并且使得abs(ai-ai1)的和最小. 如 ...

  7. Beego 和 Bee 的开发实例

    Beego不是一般的web开发包.它构建在大量已存在的Go之上,提供了许多的功能,以下是提供的功能: 一个完整的ORM 缓存 支持session 国际化(i18n) 实时监测和重载 发布支持 ==== ...

  8. visio2013 激活工具,仅供交流学习

    visio2013激活软件 环境是 win7, 64 bit 装了 visio 2013 , 可以却不能用它来画图,在网上找了一些破解工具,大都不能解决问题.网上不靠谱的广告型文章太多了 所幸,终于找 ...

  9. IO流-递归遍历目录下指定后缀名结尾的文件名称

    /* *自定义遍历目录下指定后缀名结尾文件的名称的方法: * * param file:指定目录 name:指定后缀名 */ 1 public static void FileName(File fi ...

  10. jquery.uploadify 使用过程

    HTML: <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"& ...