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.left = None
 6 #         self.right = None
 7 #
 8 # Definition for singly-linked list.
 9 # class ListNode:
10 #     def __init__(self, x):
11 #         self.val = x
12 #         self.next = None
13
14 class Solution:
15     # @param head, a list node
16     # @return a tree node
17     def sortedListToBST(self, head):
18         # special case frist
19         if head is None:
20             return None
21         if head.next is None:
22             return TreeNode(head.val)
23         # slow point & fast point trick to divide the list
24         slow = ListNode(0)
25         fast = ListNode(0)
26         slow.next = head
27         fast.next = head
28         while fast.next is not None and fast.next.next is not None:
29             slow = slow.next
30             fast = fast.next.next
31         left = head
32         right = slow.next.next
33         root = TreeNode(slow.next.val)
34         slow.next.next = None # cut the connection bewteen right child tree and root TreeNode
35         slow.next = None # cut the connection between left child tree and root TreeNode
36         root.left = self.sortedListToBST(left)
37         root.right = self.sortedListToBST(right)
38         return root

思路

binary search tree 是什么先搞清楚

由于是有序链表,所以可以采用递归的思路,自顶向下建树。

1. 每次将链表的中间节点提出来;链表中间节点之前的部分作为左子树继续递归;链表中间节点之后的部分作为右子树继续递归。

2. 停止递归调用的条件是传递过去的head为空(某叶子节点为空)或者只有一个ListNode(到某叶子节点了)。

找链表中间节点的时候利用快慢指针的技巧:注意,因为前面的special case已经将传进来为空链表和长度为1的链表都处理了,所以快慢指针的时候需要判断一下从最短长度为2的链表的处理逻辑。之前的代码在while循环中只判断了fast.next.next is not None就忽略了链表长度为2的case,因此补上了一个fast.next is not None的case,修改过一次就AC了。

网上还有一种思路,只需要走一次链表就可以完成转换,利用的是自底向上建树。下面这个日志中有说明,留着以后去看看。

http://blog.csdn.net/nandawys/article/details/9125233

时间: 2024-10-14 20:38:13

leetcode 【 Convert Sorted List to Binary Search Tree 】python 实现的相关文章

[leetcode]Convert Sorted List to Binary Search Tree @ Python

原题地址:http://oj.leetcode.com/problems/convert-sorted-list-to-binary-search-tree/ 题意:将一条排序好的链表转换为二叉查找树,二叉查找树需要平衡. 解题思路:两个思路:一,可以使用快慢指针来找到中间的那个节点,然后将这个节点作为树根,并分别递归这个节点左右两边的链表产生左右子树,这样的好处是不需要使用额外的空间,坏处是代码不够整洁.二,将排序好的链表的每个节点的值存入一个数组中,这样就和http://www.cnblog

[leetcode]Convert Sorted Array to Binary Search Tree @ Python

原题地址:http://oj.leetcode.com/problems/convert-sorted-array-to-binary-search-tree/ 题意:将一个排序好的数组转换为一颗二叉查找树,这颗二叉查找树要求是平衡的. 解题思路:由于要求二叉查找树是平衡的.所以我们可以选在数组的中间那个数当树根root,然后这个数左边的数组为左子树,右边的数组为右子树,分别递归产生左右子树就可以了. 代码: # Definition for a binary tree node # class

LeetCode: Convert Sorted List to Binary Search Tree [109]

[题目] Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. [题意] 将一个有序链表转换成平衡二叉树 [思路] 思路跟Convert Sorted Array to Binary Search Tree完全一样 [代码] /** * Definition for singly-linked list. * struct List

[LeetCode] 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. 方法:为了使BST高度平衡,要找链表中的中值作为当前根节点. /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) :

LeetCode: Convert Sorted Array to Binary Search Tree [108]

[题目] Given an array where elements are sorted in ascending order, convert it to a height balanced BST. [题意] 给定一个已排序的数组(不存在重复元素),将它转换成一棵平衡二叉搜索树. [思路] 由于平衡二叉树要求左右子树的高度差绝对值相遇等于1,也就是说左右子树尽可能包含相同数目节点. 则使用二分法来解本题即可. [代码] /** * Definition for binary tree *

[LeetCode] 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. 这道题是要求把有序链表转为二叉搜索树,和之前那道Convert Sorted Array to Binary Search Tree 将有序数组转为二叉搜索树思路完全一样,只不过是操作的数据类型有所差别,一个是数组,一个是链表.数组方便就方便在可以通过index直接访问任意一个元

LeetCode: Convert Sorted Array to Binary Search Tree 解题报告

Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascending order, convert it to a height balanced BST.SOLUTION 1:使用递归解决. base case: 当索引值超过时,返回null. 递归主体:构造根节点,调用递归构造左右子树.并将左右子树挂在根节点上. 返回值:返回构造的根节点. GITHUB: https:

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. 原题链接:https://oj.leetcode.com/problems/convert-sorted-array-to-binary-search-tree/ 题目:给定一个升序排列元素的数组,将其转换成高度平衡的二叉树. 思路:已经排序.则从中间劈开,中间元素为树的根,左右递归构建. public

LeetCode——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. 原题链接:https://oj.leetcode.com/problems/convert-sorted-list-to-binary-search-tree/ 题目:给定一个有正序链表,将其转换成一个二叉搜索树. 思路:能够依照之前的数组的思路来做.即找到中间值.再左右递归建树

[LeetCode] Convert Sorted List to Binary Search Tree DFS,深度搜索

Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. Hide Tags Depth-first Search Linked List 这题是将链表变成二叉树,比较麻烦的遍历过程,因为链表的限制,所以深度搜索的顺序恰巧是链表的顺序,通过设置好递归函数的参数,可以在深度搜索时候便可以遍历了. TreeNode * help_f(Lis