109.Convert Sorted List to Binary Search Tree Leetcode Python

Convert Sorted List to Binary Search Tree Total Accepted: 32343 Total Submissions: 117376 My Submissions Question Solution

Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST

这题和convert array to binary search tree不同点在与LinkedList 没有random access所以要想找到中点只能利用fast 和slow pointer来查找。

这种方法比直接把linkedlist转成array要慢。

代码如下:

# 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 convert(self,head,tail):
        if head==tail:
            return None
        if head.next==tail:
            return TreeNode(head.val)
        mid=head
        fast=head
        while fast!=tail and fast.next!=tail:
            fast=fast.next.next
            mid=mid.next
        node=TreeNode(mid.val)
        node.left=self.convert(head,mid)
        node.right=self.convert(mid.next,tail)
        return node
    def sortedListToBST(self, head):
        return self.convert(head,None)
        
时间: 2024-08-06 20:07:25

109.Convert Sorted List to Binary Search Tree Leetcode Python的相关文章

Convert Sorted List to Binary Search Tree leetcode java

题目: Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. 题解: 之前做过一道是从sorted array转换到BinarySearchTree的,方法还是一样二分法.但是构造树的方法不是由顶至下了,是要由低至上的建立. 代码如下: 1     static ListNode h; 2   3     public TreeNo

Convert Sorted Array to Binary Search Tree leetcode java

题目: Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 题解: 先复习下什么是二叉搜索树(引自Wikipedia): 二叉查找树(Binary Search Tree),也称有序二叉树(ordered binary tree),排序二叉树(sorted binary tree),是指一棵空树或者具有下列性质的二叉树: 若任意节点的左子树不空,则左子树

leetcode 109 Convert Sorted List to Binary Search Tree

题目连接 https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/ Convert Sorted List to Binary Search Tree Description Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. /** * De

【一天一道LeetCode】#109. Convert Sorted List to Binary Search Tree

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. (二)解题 本题大意:给定一个单向链表,构造出平衡二叉搜索树 解题思路:参考[一天一道Leet

leetcode 【 Convert Sorted List to Binary Search Tree 】python 实现

题目: Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. 代码:oj测试通过 Runtime: 178 ms 1 # Definition for a binary tree node 2 # class TreeNode: 3 # def __init__(self, x): 4 # self.val = x 5 # self

[leedcode 109] Convert Sorted List to Binary Search Tree

Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ /** * D

【LeetCode】109& - Convert Sorted List to Binary Search Tree&Convert Sorted Array to Binary Search Tree

Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. Solution 1:  recursion runtime: 28ms. /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(i

LeeCode 109.Convert Sorted List to Binary Search Tree(将排序链表转化为BST) 解题思路和方法

Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. 思路:此题与排序数组很大的不同是链表不知道长度以及上面的值.其整体思路还是中间值作为根节点,但是需要一点策略,不然就会超时. 先整体遍历长度之后,将长度保存,这样就不需要每次都遍历链表了. 代码如下: /** * Definition for singly-linked list

【Leetcode】109. Convert Sorted List to Binary Search Tree

Question: Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node n