Don't understand what's wrong with my code.


  • 0
    M

    Re: Share My Easy Understatnd Java Solution.
    public TreeNode sortedListToBST(ListNode head) {
    if(head == null){
    return null;
    }
    if(head.next == null){
    return new TreeNode(head.val);
    }
    ListNode fast = head.next;
    ListNode slow = head;
    while(fast != null && fast.next != null){
    slow = slow.next;
    fast = fast.next.next;
    }
    TreeNode node = new TreeNode(slow.val);
    node.right = sortedListToBST(slow.next);
    slow = null;
    node.left = sortedListToBST(head);
    return node;
    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.