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

看问题,首先想到的解决办法如下:

(1)单独设计一个函数可以,计算出有序链表的中间节点的前驱节点。后续会看到原因,这个函数进入的有序链表长度长度至少有2个节点;

(2)回到原来需要设计的函数:

a. 如果没有节点返回null,如果是只有一个节点,将这个节点制造成树节点返回;由于这个原因,输入到寻找前驱节点的问题的有序链表至少2个节点;

b. 找到前驱节点,找到中间节点,完成赋值。

c. 在前驱节点和中间节点之间进行切段;为了实现分治递归的用途;

d. 递归寻找左节点,右节点;返回根节点;

其中:单独设计函数非常值得一提,因为:2/2 = 1, 3/2 = 1; 4/2 = 2, 5/2 = 2; ~~~  在进行整数除法的时候;

所以,寻找前驱函数的循环设计,需要讲所有情况规约成  偶,奇 一组的样子,而且,由于起点是2,所以每次都是偶在前面,奇在后面;

public class Solution {
    ListNode getLeftNodeFromList(ListNode head){//默认就至少有2个链表节点
        ListNode next = head.next;
        ListNode index = head.next;
        ListNode pre = head;
        while(next != null)
        {
            next = next.next;
            if(next == null) break;
            next = next.next;
            if(next == null) break;
            pre = pre.next;
            index = pre.next;
        }
        return pre;
    }
    public TreeNode sortedListToBST(ListNode head) {
        if(head == null) return null;
        if(head.next == null) return new TreeNode(head.val);
        ListNode left = getLeftNodeFromList(head);
        ListNode mid = left.next;
        left.next = null;
        TreeNode root = new TreeNode(mid.val);
        root.left = sortedListToBST(head);
        root.right = sortedListToBST(mid.next);
        return root;
    }
}

当然,这个办法不是最好的,稍后会讨论最优解,这个解由于利用递归,时间复杂度为:O(N*lgN)

时间: 2024-11-20 19:07:57

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

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

[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 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

LeetCode 108. Convert Sorted Array to Binary Search Tree (有序数组转化为二叉搜索树)

Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 题目标签:Tree 这道题目给了我们一个有序数组,从小到大.让我们把这个数组转化为height balanced BST. 首先来看一下什么是binary search tree: 每一个点的left < 节点 < right, 换一句话说,每一个点的值要大于左边的,小于右边的. 那么什么是heigh

[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

【Leetcode】109. Convert Sorted List to Binary Search Tree

Question: 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 n