Problem Link:

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

Same idea to Convert Sorted Array to Binary Search Tree, but we use a recursive function to construct the binary search tree.

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

【LeetCode OJ】Convert Sorted Array to Binary Search Tree的更多相关文章

  1. 【LeetCode OJ】Convert Sorted List to Binary Search Tree

    Problem Link: http://oj.leetcode.com/problems/convert-sorted-list-to-binary-search-tree/ We design a ...

  2. LeetCode OJ 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. 把一 ...

  3. LeetCode OJ: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

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

  5. Leetcode No.108 Convert Sorted Array to Binary Search Tree(c++实现)

    1. 题目 1.1 英文题目 Given an integer array nums where the elements are sorted in ascending order, convert ...

  6. 【leetcode】Convert Sorted Array to Binary Search Tree (easy)

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

  7. 【题解】【BST】【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.思路: ...

  8. 【Leetcode】【Medium】Convert Sorted Array to Binary Search Tree

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

  9. 【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. 题解 ...

随机推荐

  1. 回传数据startActivityForResult()

    1.调用者Activity01开启新的界面选用startActivityForResult(intent,requestCode);在Activity01中Intent intent=new Inte ...

  2. C中字符串的几种定义方法及说明

    在C中定义字符串有下列几种形式:字符串常量,char数组,char指针 1.字符串常量 即:位于一对双括号中的任何字符.双引号里的字符加上编译器自动提供的结束标志\0字符,作为 一个字符串存储在内存中 ...

  3. 字典的循环和if语句

    字典是键-值(key-value)存储,循环的时候也是以键为对象 d = {'Michael': 95, 'Tracy': 85,'Bob': 75} for x in d : print x 输出结 ...

  4. firefox阅读模式

    并不是所有的网页都可以转换为阅读模式的,所以遇到一些识别不了的网页时,可以在地址栏输入“about:reader?url=网址”后回车即可.

  5. Sprint(第三天11.16)

    Sprint1第一阶段 1.类名:软件工程-第一阶段 2.时间:11.14-11.23 3.选题内容:点餐系统 4.团队博客地址:http://www.cnblogs.com/iamCarson/ 团 ...

  6. <a>每次点击都会让浏览器重新打开一个窗口问题

    <a> 标签的 target 属性规定在何处打开链接文档.如果在一个 <a> 标签内包含一个 target 属性,浏览器将会载入和显示用这个标签的 href 属性命名的.名称与 ...

  7. C#综合笔记

    AspNetPager分页控件 UrlPaging="true" 利用get方式page?=1进行分页. UrlPaging="false"利用post方式进行 ...

  8. 安装 SciPy 和 scikit-learn 升级pip 及pip基本命令表

    安装Scipy和scikit-learn出错,出错记录及当时最终解决办法 问题: 一.使用pip直接安装 直接pip install scipy C:\Users\Plain>pip insta ...

  9. [原创] WINDOWS 7 精简教程之驱动精简 可用于64和32

    追风神话 发表于 2014-9-1 11:35:56   https://www.itsk.com/forum.php?mod=viewthread&tid=334491&highli ...

  10. 带选择的sql简单用法

    一般写法: select * from itcast_topic order by (case type when 2 then 2 else 1 end ) desc ,postTime desc ...