Convert Sorted List to Binary Search Tree LeetCode

原创 2016年08月29日 11:57:58


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;
    }
}


相关文章推荐

LeetCode_Convert Sorted List to Binary Search Tree

LeetCode_Convert Sorted List to Binary Search Tree 解题思路

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

LeetCode Convert Sorted List to Binary Search Tree 解题报告

从给定的有序链表生成一颗平衡二叉树。 解题思路:最容易想到的就是利用数组生成二叉树的方法,找到中间节点作为二叉树的root节点,然后分别对左右链表递归调用分别生成左子树和右子树。时间复杂度O(N*lg...

LeetCode 108, 109. Convert Sorted Array/List to Binary Search Tree

1. 题目描述 Given an array where elements are sorted in ascending order, convert it to a height bal...

Convert Sorted List to Binary Search Tree(leetcode)

题目:

leetcode_108 Convert Sorted List to Binary Search Tree

题目分析: 给定一个升序的单链表,将其转换为一个高度平衡的二分查找树(BST)。 解题思路: 递归实现二叉查找树定义二叉查找树又称有序树,是一棵空树或者具有以下性质的二叉树:1)若任意节点的左子树不为...

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 bala...

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 bala...

109 Convert Sorted List to Binary Search Tree [Leetcode]

题目内容: Given a singly linked list where elements are sorted in ascending order, convert it to a hei...

LeetCode OJ:Convert Sorted List to Binary Search Tree

Convert Sorted List to Binary Search Tree   Given a singly linked list where elements are sorted ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Convert Sorted List to Binary Search Tree LeetCode
举报原因:
原因补充:

(最多只允许输入30个字)