Convert Sorted List to Binary Search Tree

来源:互联网 发布:淘宝女用催情药 编辑:程序博客网 时间:2024/06/06 05:42

Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.

和上一题不同的地方就在于这一题是sorted list 上一题是 sorted array

比较无耻点,其实可以直接仿照之前的sorted array的方法:

代码如下(有问题):

/** * 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 {    public TreeNode sortedListToBST(ListNode head) {        if(head==null) return null;        int count = 0;        while(head!=null) {            count++;        }        return sortedListToBST(head,0,count-1);    }    private TreeNode sortedListToBST(ListNode head, int start, int end) {        if(start>end) return null;        int mid = (start+end)/2;        ListNode midNode = head;        int i = 0;        while(i<mid) {            midNode = midNode.next;            i++;        }        TreeNode root = new TreeNode(midNode.val);        root.left = sortedListToBST(head,start, mid-1);        root.right = sortedListToBST(head,mid+1,end);        return root;    }}
一开始死活找不到问题在哪里,后来发现,真是傻了。
</pre><p></p><p><span style="font-family:Helvetica Neue, Helvetica, Arial, sans-serif;color:#333333;"><span style="font-size: 14px; line-height: 30px;">修改后的代码如下:</span></span></p><p><span style="font-family:Helvetica Neue, Helvetica, Arial, sans-serif;color:#333333;"><span style="font-size: 14px; line-height: 30px;"></span></span></p><p><span style="font-family:Helvetica Neue, Helvetica, Arial, sans-serif;color:#333333;"><span style="font-size: 14px; line-height: 30px;"></span></span></p><pre name="code" class="java"><span style="font-family:Helvetica Neue, Helvetica, Arial, sans-serif;color:#333333;">/** * 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 {    public TreeNode sortedListToBST(ListNode head) {        if(head==null) return null;        int count = 0;        ListNode p = head;        while(p!=null) {            p = p.next;            count++;        }        return sortedListToBST(head,0,count-1);    }    private TreeNode sortedListToBST(ListNode head, int start, int end) {        if(start>end) return null;        int mid = (start+end)/2;        ListNode midNode = head;        int i = 0;        while(i<mid) {            midNode = midNode.next;            i++;        }        TreeNode root = new TreeNode(midNode.val);        root.left = sortedListToBST(head,start, mid-1);        root.right = sortedListToBST(head,mid+1,end);        return root;    }}</span>

但是发现 too young too simple。leetcode设置了个巨长的list。。。直接TLE了。but,代码应该没问题吧?写了两个测试用例,跑出的都没问题。

一怒之下,用了个更无耻的方法,把list直接全部转为数组。哈哈哈哈,果然过了。

/** * 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 {    public TreeNode sortedListToBST(ListNode head) {        if(head==null) return null;        int count = 0;        List list = new ArrayList();        ListNode p = head;        while(p!=null) {            list.add(p.val);            p = p.next;            count++;        }        return sortedListToBST(list,0,count-1);    }    private TreeNode sortedListToBST(List list, int start, int end) {        if(start>end) return null;        int mid = (start+end)/2;        TreeNode root = new TreeNode((int)list.get(mid));        root.left = sortedListToBST(list,start, mid-1);        root.right = sortedListToBST(list,mid+1,end);        return root;    }}


那么有什么不那么无耻的改进的方法么?一样的想法,从上往下 消耗太大,从下往上应该就还好:


/** * 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 list;    public TreeNode sortedListToBST(ListNode head) {        if(head==null) return null;        int count = 0;        ListNode p = head;        while(p!=null) {            p = p.next;            count++;        }        list = head;        return sortedListToBST(head,0,count-1);    }    private TreeNode sortedListToBST(ListNode head, int start, int end) {        if(start>end) return null;        int mid = (start+end)/2;        TreeNode leftChild = sortedListToBST(head,start,mid-1);        TreeNode root = new TreeNode(list.val);        root.left = leftChild;        list = list.next;        TreeNode rightChild = sortedListToBST(head,mid+1, end);        root.right = rightChild;        return root;    }}


0 0
原创粉丝点击