码迷,mamicode.com
首页 > 其他好文 > 详细

#Leet Code# Convert Sorted Array to Binary Search Tree

时间:2014-07-29 16:41:11      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   代码   div   ar   log   

描述:递归

代码:

 1 class Solution:
 2     # @param num, a list of integers
 3     # @return a tree node
 4     def sortedArrayToBST(self, num):
 5         if len(num) == 0:
 6             return None
 7 
 8         mid_index = len(num) / 2
 9 
10         tmp_tree = TreeNode(num[mid_index])
11         tmp_tree.left = self.sortedArrayToBST(num[:mid_index])
12         tmp_tree.right = self.sortedArrayToBST(num[mid_index + 1:])
13 
14         return tmp_tree

 

#Leet Code# Convert Sorted Array to Binary Search Tree,布布扣,bubuko.com

#Leet Code# Convert Sorted Array to Binary Search Tree

标签:style   blog   color   io   代码   div   ar   log   

原文地址:http://www.cnblogs.com/mess4u/p/3875364.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!