给定一个单链表,其中的元素按升序排序,将其转换为高度平衡的二叉搜索树。
给定的有序链表: [-10, -3, 0, 5, 9],
一个可能的答案是:[0, -3, 9, -10, null, 5], 它可以表示下面这个高度平衡二叉搜索树:
0
/ \
-3 9
/ /
-10 5
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def sortedListToBST(self, head: ListNode) -> TreeNode:
# 采用递归的方式进行求解,由于是已经排序好的单链表,首先获取全部的排序元素值,然后根据取中间元素构建根节点,依次进行递归迭代左右结点即可
def helper(vals):
if vals:
mid = len(vals) // 2
root = TreeNode(vals[mid])
root.left = helper(vals[:mid])
root.right = helper(vals[mid+1:])
return root
if not head:
return
# 获取所有的元素值
vals = []
while head:
vals.append(head.val)
head = head.next
return helper(vals)
上述解决问题的思路可以说一种取巧的思路,那么我们能够直接操作单链表来查询中间结点呢?答案是,当然可以。为了直接操作单链表来获取中间结点,我们首先要明确怎么来进行遍历查找。
Python版本
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def findMiddle(self, head):
# pre指针用来将左边从中间结点断开
prev = None
slow = head
fast = head
# 迭代执行直到尾指针到达链表结尾
while fast and fast.next:
prev = slow
slow = slow.next
fast = fast.next.next
# 从中间结点进行断开
if prev:
prev.next = None
return slow
def sortedListToBST(self, head):
"""
:type head: ListNode
:rtype: TreeNode
"""
# 如果头结点不存在,直接返回空
if not head:
return None
# 找到中间结点
mid = self.findMiddle(head)
# 将中间结点构建成根结点
node = TreeNode(mid.val)
# 如果只有一个元素,则直接返回
if head == mid:
return node
# 递归迭代执行,构建左右子树
node.left = self.sortedListToBST(head)
node.right = self.sortedListToBST(mid.next)
return node
Java版本
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public TreeNode sortedListToBST(ListNode head) {
if(head == null)
return null;
ListNode mid = this.findMiddle(head);
TreeNode node = new TreeNode(mid.val);
if(head == mid)
return node;
node.left = this.sortedListToBST(head);
node.right = this.sortedListToBST(mid.next);
return node;
}
public ListNode findMiddle(ListNode node){
if(node == null)
return null;
ListNode pre = null;
ListNode slow = node;
ListNode fast = node;
while(fast != null && fast.next != null){
pre = slow;
slow = slow.next;
fast = fast.next.next;
}
if(pre != null)
pre.next = null;
return slow;
}
}