Convert Sorted List to Binary Search Tree LeetCode

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 {
    private ListNode current;
    public int getListLength(ListNode head) {
        if (head == null) {
            return 0;
        }
        int size = 0;
        ListNode temp = head;
        while (temp != null) {
            size++;
            temp = temp.next;
        }
        return size;
    }
    public TreeNode getCurrentTreeNodeHelper(int size) {
        if (size == 0) {
            return null;
        }
        TreeNode left = getCurrentTreeNodeHelper(size / 2);
        TreeNode root = new TreeNode(current.val);
        current  = current.next;
        TreeNode right = getCurrentTreeNodeHelper(size - 1 - size / 2);

        root.left = left;
        root.right = right;
        return root;
    }
    public TreeNode sortedListToBST(ListNode head) {
        current = head;
        int size = getListLength(head);
        TreeNode dummy = getCurrentTreeNodeHelper(size);

        return dummy;
    }
}
时间: 2024-10-10 01:51:14

Convert Sorted List to Binary Search Tree LeetCode的相关文章

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. 题解: 之前做过一道是从sorted array转换到BinarySearchTree的,方法还是一样二分法.但是构造树的方法不是由顶至下了,是要由低至上的建立. 代码如下: 1     static ListNode h; 2   3     public TreeNo

Convert Sorted Array to Binary Search Tree leetcode java

题目: Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 题解: 先复习下什么是二叉搜索树(引自Wikipedia): 二叉查找树(Binary Search Tree),也称有序二叉树(ordered binary tree),排序二叉树(sorted binary tree),是指一棵空树或者具有下列性质的二叉树: 若任意节点的左子树不空,则左子树

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

Convert Sorted Array to Binary Search Tree——LeetCode

Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 题目大意:给定一个升序序列的数组,将其转换为二叉搜索树. 解题思路:数组中间元素是根元素,根元素将数组划分为两部分,两个部分的中心元素分别为根元素的左右孩子,依次递推...可用迭代或递归来做. 解法一(迭代):迭代需要自己额外记录下标 public TreeNode sortedArrayToBST(i

Convert Sorted List to Binary Search Tree [leetcode] O(n)的算法

主要的思想类似中序遍历,先构建左子树,再构建当前节点,并构建右子树 TreeNode *sortedListToBST(ListNode *head) { int count = 0; ListNode * cur = head; while (cur) { count++; cur = cur->next; } return sortedListToBST(head, count); } TreeNode *sortedListToBST(ListNode * (&head), int c

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

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

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

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

原题地址:http://oj.leetcode.com/problems/convert-sorted-array-to-binary-search-tree/ 题意:将一个排序好的数组转换为一颗二叉查找树,这颗二叉查找树要求是平衡的. 解题思路:由于要求二叉查找树是平衡的.所以我们可以选在数组的中间那个数当树根root,然后这个数左边的数组为左子树,右边的数组为右子树,分别递归产生左右子树就可以了. 代码: # Definition for a binary tree node # class