【原创】leetCodeOj ---Convert Sorted List to Binary Search Tree 解题报告

原题地址:

https://oj.leetcode.com/problems/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.

方法:

单纯。如何安排插入顺序,使得一棵二叉排序树接近平衡?

你从中间开始插嘛。这样左子树和右子树之差或者为0,或者为1。

所以,这个问题的本质是找链表的中间节点。

找到中间节点后,递归产生左子树和右子树,在找左边子链表的中间节点和右边子链表的中间节点即可。

当然啦,最后返回中间节点。

全部代码:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; next = null; }
 * }
 */
/**
 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public TreeNode sortedListToBST(ListNode head) {
        int len = countLen(head);
        if (len == 0)
            return null;
        return constructTree(head,len); // len is the number of nodes in list.
    }

    private TreeNode constructTree(ListNode head,int len)
    {
        if (head == null || len <= 0)
            return null;
        int mid = len / 2 + 1;
        TreeNode tmp = new TreeNode(head.val);
        tmp.left = null;
        tmp.right = null;
        if (mid == 0)
            return tmp;
        ListNode tar = findMidNode(head,mid);
        tmp.val = tar.val;
        tmp.left = constructTree(head,mid - 1);
        tmp.right = constructTree(tar.next,len - mid);
        return tmp;
    }

    private ListNode findMidNode(ListNode head,int mid)
    {
        int count = 1;
        while (mid != count++)
            head = head.next;
        return head;
    }

    private int countLen(ListNode head)
    {
        int len = 0;
        while (head != null)
        {
            len ++;
            head = head.next;
        }
        return len;
    }
}
时间: 2024-12-09 00:18:39

【原创】leetCodeOj ---Convert Sorted List to Binary Search Tree 解题报告的相关文章

LeetCode: Convert Sorted Array to Binary Search Tree 解题报告

Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascending order, convert it to a height balanced BST.SOLUTION 1:使用递归解决. base case: 当索引值超过时,返回null. 递归主体:构造根节点,调用递归构造左右子树.并将左右子树挂在根节点上. 返回值:返回构造的根节点. GITHUB: https:

【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. [解析] 分治:用快慢指针找到链表的中点,作为树的root,然后二分--中点之前的链表和中点之后的链表分别再构造二叉平衡树. /** * Definition for singly-linked list. * public class ListNode { * int

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. 从给定的有序链表生成平衡二叉树. 解题思路:最容易想到的就是利用数组生成二叉树的方法,找到中间节点作为二叉树的root节点,然后分别对左右链表递归调用分别生成左子树和右子树.时间复杂度O(N*lgN) AC代码: public class Solution { ListNode

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

leetcode 108 Convert Sorted Array to Binary Search Tree

题目连接 https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/ Convert Sorted Array to Binary Search Tree Description Given an array where elements are sorted in ascending order, convert it to a height balanced BST. /** * Definition f

39.1: Convert Sorted Array to Binary Search Tree

/************************************************************************/            /*       39.1:  Convert Sorted Array to Binary Search Tree                               */            /************************************************************

Convert Sorted Array to Binary Search Tree &amp; Convert Sorted List to Binary Search Tree

Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 方法:将有序数组转为平衡二叉树,我们知道平衡二叉树的中序遍历是有序数组,而且“中间”的元素可以作为根节点,那么构建树的时候,找出中间元素,构造根元素,然后继续重复上面的方法,构造左子树和右子树.代码如下: 1 /**

leetcode dfs Convert Sorted List to Binary Search Tree

Convert Sorted List to Binary Search Tree Total Accepted: 21420 Total Submissions: 78476My Submissions Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. 题意:把有序单链表转换为二叉查找树 思路: 用链表中心作为当作二叉树的根,

[leetcode]Convert Sorted List to Binary Search Tree @ Python

原题地址:http://oj.leetcode.com/problems/convert-sorted-list-to-binary-search-tree/ 题意:将一条排序好的链表转换为二叉查找树,二叉查找树需要平衡. 解题思路:两个思路:一,可以使用快慢指针来找到中间的那个节点,然后将这个节点作为树根,并分别递归这个节点左右两边的链表产生左右子树,这样的好处是不需要使用额外的空间,坏处是代码不够整洁.二,将排序好的链表的每个节点的值存入一个数组中,这样就和http://www.cnblog