Leetcode: Convert sorted list to Binary Search Tree

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

Difficulty 70, similar to the convert sorted array to binary search tree problem. The practice of this question is that we need to use the dot to make the root of each layer of tree. The problem is that, for a linked list, we cannot access its intermediate elements at constant time. So my idea is to spend O (n) Time First saving the elements in the linked list to an array or arraylist, so that there is a sorted array or arraylist, we can easily recursively create a height balanced BST.

 1 /** 2  * Definition for singly-linked list. 3  * public class ListNode { 4  *     int val; 5  *     ListNode next; 6  *     ListNode(int x) { val = x; next = null; } 7  * } 8  */ 9 /**10  * Definition for binary tree11  * public class TreeNode {12  *     int val;13  *     TreeNode left;14  *     TreeNode right;15  *     TreeNode(int x) { val = x; }16  * }17  */18 public class Solution {19     public TreeNode sortedListToBST(ListNode head) {20         if (head == null) return null;21         ListNode dummy = new ListNode(-1);22         dummy.next = head;23         ListNode cursor = dummy;24         int count = 0;25         while (cursor.next != null) {26             count++;27             cursor = cursor.next;28         }29         int[] array = new int[count];30         cursor = dummy;31         for (int i = 0; i < count; i++) {32             array[i] = cursor.next.val;33             cursor = cursor.next;34         }35         TreeNode result = helper(array, 0, array.length - 1);36         return result;37     }38     39     public TreeNode helper(int[] array, int begin, int end) {40         if (begin > end) return null;41         if (begin == end) return new TreeNode(array[begin]);42         int mid = (begin + end) / 2;43         TreeNode root = new TreeNode(array[mid]);44         root.left = helper(array, begin, mid - 1);45         root.right = helper(array, mid + 1, end);46         return root;47     }48 }

Using arraylist:

 1 /** 2  * Definition for singly-linked list. 3  * public class ListNode { 4  *     int val; 5  *     ListNode next; 6  *     ListNode(int x) { val = x; next = null; } 7  * } 8  */ 9 /**10  * Definition for binary tree11  * public class TreeNode {12  *     int val;13  *     TreeNode left;14  *     TreeNode right;15  *     TreeNode(int x) { val = x; }16  * }17  */18 public class Solution {19     public TreeNode sortedListToBST(ListNode head) {20         if (head == null) return null;21         22         ArrayList<Integer> listNode = new ArrayList<Integer>();23         while(head != null){24             listNode.add(head.val);25             head = head.next;26         }27         int left = 0;28         int right= listNode.size()-1;29         return helper(listNode, left, right);30     }31     32     public TreeNode helper(ArrayList<Integer> listNode, int left, int right){33         if(left>right) return null;34         35         TreeNode node = new TreeNode(0);36         int mid = (left+right)/2;37         node.val = listNode.get(mid);38         node.left = helper(listNode, left, mid-1);39         node.right = helper(listNode, mid+1, right);            40         return node;41     }42 }

 

Leetcode: Convert sorted list to Binary Search Tree

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.