# Definition for a binary tree node # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None # # Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = None class Solution: # @param head, a list node # @return a tree node def AtoBST(self,A): n=len(A) if n==0: return None else: ml=n//2 A[ml].right=self.AtoBST(A[ml+1:]) A[ml].left=self.AtoBST(A[:ml]) return A[ml] def sortedListToBST(self, head): A=[] while head: A.append(TreeNode(head.val)) head=head.next return self.AtoBST(A)
Friday, September 12, 2014
Leetcode: Convert Sorted List to Binary Search Tree @Python
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment