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

本文探讨了如何将有序链表转换为二叉搜索树的过程,通过使用双指针法找到链表的中点,从而实现有序数据结构到二叉搜索树的有效映射。

The problem: 

Convert Sorted List to Binary Search Tree

 

Link:

https://oj.leetcode.com/problems/convert-sorted-list-to-binary-search-tree/

 

My analysis:

The idea of building a binary tree from a sorted linked list is same as from a sorted array.
The key issues here is:
1. locate the mid element of linked list
2. split the linked list into two parts according mid element.

The classic way of finding the mid element on a linked list is to use two pointers: walker and runner.

while (runner.next != null && runner.next.next != null) {
            
            pre_walker = walker;
            walker = walker.next;
            runner = runner.next.next;
}

Note: the mid element would be the node pointer by walker, to split the list into two parts, we have to keep a pre_walker pointer to point the previous element of walker.

One pitfall.
The corner case: when there are only two elements in the list.
In this situation, we take the first element as root, the second element as the right child.
In this case, the above invariant could not be used, we should tackle it specically. And, we could detect this case by testing if pre_walker has already been initialized.

if (pre_walker == null) {
    head1 = null;
} else { 
    head1 = head;
    pre_walker.next = null; 
}

Then the problem was fixed!

 

My solution:

public class Solution {
    public TreeNode sortedListToBST(ListNode head) {
        
        if (head == null)
            return null;
            
        return helper(head);
    }
    
    private TreeNode helper(ListNode head) {
    
        if (head == null)
            return null;

        if (head.next == null)
            return new TreeNode(head.val);
            
        ListNode head1;
        ListNode head2;
        ListNode walker = head;
        ListNode runner = head;
        ListNode pre_walker = null; //use to record the pre node of walker pointer
        ListNode mid;

        while (runner.next != null && runner.next.next != null) {
            
            pre_walker = walker;
            walker = walker.next;
            runner = runner.next.next;
        }
        
        mid = walker; //the mid position is pointed by walker
        TreeNode ret = new TreeNode(mid.val);
        
        /*
        if pre_walker has already been initialized, there are at list three elements left in the list.
        */
        if (pre_walker == null) { //careful! only two elements left, we choose the head as mid element.
            head1 = null;
        } else { 
            head1 = head;
            pre_walker.next = null; //split the original linked list at walker. 
        }
        
        head2 = mid.next;
        ret.left = helper(head1);
        ret.right = helper(head2);
        
        return ret; 
    }
}

 

转载于:https://www.cnblogs.com/airwindow/p/4209797.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值