Middle-题目56:109. Convert Sorted List to Binary Search Tree

原创 2016年05月31日 16:26:58

题目原文:
iven a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.
题目大意:
把单链表转换成二叉平衡树。
题目分析:
把单链表转换成数组,再调用Middle-题目15的函数就可以了。如果要直接做的话,使用快慢指针法找到中点,再递归即可。
源码:(language:java)

public class Solution {
    public TreeNode sortedListToBST(ListNode head) {
        List<Integer> list = new ArrayList<Integer>();
        for(ListNode temp = head;temp!=null;temp=temp.next)
            list.add(temp.val);
        Integer[] nums = new Integer[list.size()];
        return sortedArrayToBST(list.toArray(nums));
    }
    public TreeNode sortedArrayToBST(Integer[] nums) {
        if(nums.length==0)
            return null;
        TreeNode bst=createBST(nums,0,nums.length-1);
        return bst;
    }
    public TreeNode createBST(Integer[] nums,int start,int end) {
        int mid = (start + end) / 2;
        TreeNode root = new TreeNode(nums[mid]);
        if(start < mid ) 
            root.left = createBST(nums,start,mid-1);
        if(mid  < end) 
            root.right = createBST(nums,mid+1,end); 
        return root;
    }
}

成绩:
4ms,beats 1.06%,众数1ms, 59.87%

版权声明:完整版Leetcode题解请出门左转https://github.com/cmershen1/leetcode/tree/master/docs

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

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-109-Convert Sorted List to Binary Search Tree

#include using namespace std; /* 题目要求: Given a singly linked list where elements are sorted in ...

LeetCode - 109. Convert Sorted List to Binary Search Tree

同样是构建平衡的BST,这道题目的思路与上一题是一样的,都是通过递归调用的方式逐步找到每一个子树的root,然后添加左子树和右子树。因为这里数字的载体由array变成了linked list,所以也变...

Convert Sorted List to Binary Search Tree - LeetCode 109

题目描述: Given a singly linked list where elements are sorted in ascending order, convert it to a heig...
  • bu_min
  • bu_min
  • 2015年06月10日 21:07
  • 255

【LeetCode-面试算法经典-Java实现】【109-Convert Sorted List to Binary Search Tree(排序链表转换成二叉排序树)】

【109-Convert Sorted List to Binary Search Tree(排序链表转换成二叉排序树)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题  G...

LeetCode- 108/109. Convert Sorted Array/List to Binary Search Tree (JAVA)

LeetCode- 108/109. Convert Sorted Array/List to Binary Search Tree (JAVA),109. Convert Sorted List ...

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

LeetCode109 Convert Sorted List to Binary Search Tree

详细见:leetcode.com/problems/convert-sorted-list-to-binary-search-tree Java Solution: github pa...
  • zxwtry
  • zxwtry
  • 2017年04月29日 18:55
  • 107
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Middle-题目56:109. Convert Sorted List to Binary Search Tree
举报原因:
原因补充:

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