convert sorted list to binary search tree(将有序链表转成平衡二叉搜索树)

2023-04-25,,

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 never differ by more than 1.

Example:

Given the sorted linked list: [-10,-3,0,5,9],

One possible answer is: [0,-3,9,-10,null,5], which represents the following height balanced BST:

      0
/ \
-3 9
/ /
-10 5

对于将有序的数组转换成平衡二叉搜索树可以使用递归,将中间元素作为根节点,左边元素作为左子树,右边元素作为右子树。从上面的例子看出,当个数为偶数个时,作为根节点的时偏右边的那个中间元素。如-10,-3.是-3作为根节点

所以这个题也是一样,选取链表中间元素作为根节点,递归左右半部分作为左右子树。

因为一开始不会对链表进行这个处理,所以自己将链表遍历到数组中,对数组进行操作简单多了。但是本题很明显要顺便考察对链表的操作,所以后面会参考别人对链表进行直接操作。但是如果真的不会操作链表,就直接转成数组吧,毕竟,做出来最重要。下面先给出转成数组的操作。

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode sortedListToBST(ListNode head) {
if(head==null) return null;
int len=0;
ListNode node=head;
while(node!=null){
len++;
node=node.next;
}
int[] nums=new int[len];
node=head;
for(int i=0;i<len;i++){
nums[i]=node.val;
node=node.next;
}
return helper(nums,0,len-1); }
public TreeNode helper(int[] nums,int start,int end){
if(start>end) return null;
int mid=(start+end+1)/2; //偶数个元素的时候,选择偏右的那个元素作为根节点。
TreeNode root=new TreeNode(nums[mid]);
root.left=helper(nums,start,mid-1);
root.right=helper(nums,mid+1,end);
return root;
}
}

下面是直接对链表操作。用一个快指针和一个慢指针,快指针是慢指针的两倍。当快指针走到最后一个(奇数个元素),慢指针走到中间,当快指针走到最后元素的后一个(偶数个元素时)慢指针走到中间两个元素偏右那一个,符合上面例子的情况。见代码。

 public TreeNode sortedListToBST(ListNode head) {
if(head==null) return null;
return helper(head,null); //开头跟结尾,开头知道,结尾就用最后一个节点的next
}
//tail表示要生成树的节点的最后一个节点的next节点。
public TreeNode helper(ListNode head,ListNode tail){
if(head==tail) return null;
ListNode fast=head;
ListNode slow=head;
//将fast移到最后一个(tail前一个)或者是最后一个的下一个节点(tail)。此时slow移到中间元素或者中间偏右的那个元素(偶数个元素时)。
while(fast!=tail&&fast.next!=tail){
fast=fast.next.next;
slow=slow.next;
}
TreeNode root=new TreeNode(slow.val);
root.left=helper(head,slow);
root.right=helper(slow.next,tail);
return root; }

convert sorted list to binary search tree(将有序链表转成平衡二叉搜索树)的相关教程结束。

《convert sorted list to binary search tree(将有序链表转成平衡二叉搜索树).doc》

下载本文的Word格式文档,以方便收藏与打印。