【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 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的更多相关文章
- 【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 ...
- 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. 把一 ...
- 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. 讲一 ...
- 【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 ...
- 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 ...
- 【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. 有序 ...
- 【题解】【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.思路: ...
- 【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. 解题 ...
- 【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. 题解 ...
随机推荐
- LA 4287 等价性证明
题目链接:http://vjudge.net/contest/141990#overview 题意是告诉你有n个命题,m条递推关系,表示某个命题可以推出另外一个命题. 现在问你至少在增加多少个递推关系 ...
- 自定义Dialog
功能:从底部弹出的对话框,加入动画 步骤:1 定义dialog布局文件 2 设置标题,透明度style.xml,选择器selector.xml ,圆角shape.xml 等样式文件 3 设置显示位置, ...
- Dialog , ProgressDialog , PopWindow 区别
本质区别: Dialog:非阻塞对话框,弹出对话框时时,后台还可以做事情,点击背景时,对话框消失 ProgressDialog:带有圆形进度或者条形进度的对话框,一般结合handler使用.任务完成后 ...
- Python:C语言扩展
1. 概述 Python 可以非常方便地和 C 进行相互的调用. 一般,我们不会使用 C 去直接编写一个 Python 的模块.通常的情景是,我们需要把 C 的相关模块包装一下,然后在 Python ...
- Linux文件管理相关命令
Linux文件管理相关命令 作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 在了解了Linux文件管理背景知识之后, 我们可以 ...
- [ubuntu14.04 amd64 ]搜狗拼音輸入法安裝
这个网址下载之后,双击下载的deb文件http://mirrors.sohu.com/deepin/pool/non-free/f/fcitx-sogoupinyin-release/ 就会在ubun ...
- oracle 语句 字段拼接(可换行)
eg: update tablename t set t.nr = '"+NR1+"' || '<br/>' ||'"+NR2+"' where 条 ...
- 常用JavaBean:JdbcBean codes:Java通过JDBC 连接 Mysql 数据库
package bean;import java.sql.*;import com.mysql.jdbc.PreparedStatement;public class JdbcBean { publi ...
- 用c语言编写直接插入法
#include<stdio.h> //直接插入法 void D_insert(int s[],int n); int main() { int i; ]; printf("pl ...
- 关于如何来构造一个String类
今天帮着一位大二的学弟写了一个String的类,后来一想这个技术点,也许不是什么难点,但是还是简单的记录一些吧! 为那些还在路上爬行的行者,剖析一些基本的实现..... 内容写的过于简单,没有涉及到其 ...