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.
 * 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; }
 * }
 */
public class Solution {
    public TreeNode sortedListToBST(ListNode head) {
    	/**
    	 * 先计算出链表的整体长度
    	 * 然后得出中间值,将中间值作为根节点
    	 * 中间值的前的链表截断作为左子树
    	 * 中间值后的链表截断作为右子树
    	 */
        int len = 0;
        ListNode p = head;
        while(head != null){
            head = head.next;
            len++;
        }
        return BST(p,len);
    }
    /**
     * head 头结点
     * len 链表长度
     */
    private TreeNode BST(ListNode head, int len){
        if(len <= 0){
            return null;
        }
        if(len == 1){
            return new TreeNode(head.val);
        }
        int mid = 0;
        ListNode p = head;
        ListNode nextHead = null;
        //得到中间值
        while(++mid < len/2){
            p = p.next;
        }
        nextHead = p.next;//根节点
        p.next = null;
        TreeNode root = new TreeNode(nextHead.val);
        //左右子树
        root.left = BST(head,mid);
        root.right = BST(nextHead.next,len-mid-1);
        return root;
    }

}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-10 23:05:37

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

109. Convert Sorted List to Binary Search Tree 将一个有序链表转化成BST

看问题,首先想到的解决办法如下: (1)单独设计一个函数可以,计算出有序链表的中间节点的前驱节点.后续会看到原因,这个函数进入的有序链表长度长度至少有2个节点: (2)回到原来需要设计的函数: a. 如果没有节点返回null,如果是只有一个节点,将这个节点制造成树节点返回:由于这个原因,输入到寻找前驱节点的问题的有序链表至少2个节点: b. 找到前驱节点,找到中间节点,完成赋值. c. 在前驱节点和中间节点之间进行切段:为了实现分治递归的用途: d. 递归寻找左节点,右节点:返回根节点: 其中:

[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-面试算法经典-Java实现】【109-Convert Sorted List to Binary Search Tree(排序链表转换成二叉排序树)】

[109-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. 题目大意 给定一个升序的单链表.将它转换成一颗高度平衡的二叉树 解题思路 解法

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

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 t

【一天一道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

Java for LeetCode 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. 解题思路: 同上题,JAVA实现如下: public TreeNode sortedListToBST(ListNode head) { ArrayList<Integer> list=new ArrayList<Integer>(); while(head!=nu

[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&amp; - Convert Sorted List to Binary Search Tree&amp;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