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 balanced BST.
For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.
Example:
Given the sorted linked list: [-10,-3,0,5,9], One possible answer is: [0,-3,9,-10,null,5], which represents the following height balanced BST: 0 / \ -3 9 / / -10 5
Approach #1: C++. [recursive]
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: TreeNode* sortedListToBST(ListNode* head) { if (head == NULL) return NULL; ListNode* midPtr = findMiddleElement(head); TreeNode* root = new TreeNode(midPtr->val); if (head == midPtr) return root; root->left = sortedListToBST(head); root->right = sortedListToBST(midPtr->next); return root; } private: ListNode* findMiddleElement(ListNode* head) { ListNode* prevPtr = NULL; ListNode* slowPtr = head; ListNode* fasterPtr = head; while (fasterPtr != NULL && fasterPtr->next != NULL) { prevPtr = slowPtr; slowPtr = slowPtr->next; fasterPtr = fasterPtr->next->next; } // break the List Table. if (prevPtr != NULL) prevPtr->next = NULL; return slowPtr; } };
Approach #2: Java. [recursive + convertion 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; } * } */ class Solution { private List<Integer> values; public Solution() { this.values = new ArrayList<Integer>(); } private void mapListToArray(ListNode head) { while (head != null) { values.add(head.val); head = head.next; } } private TreeNode convertListToBST(int left, int right) { if (left > right) return null; int mid = left + (right - left) / 2; TreeNode root = new TreeNode(this.values.get(mid)); if (left == right) return root; root.left = convertListToBST(left, mid-1); root.right = convertListToBST(mid+1, right); return root; } public TreeNode sortedListToBST(ListNode head) { mapListToArray(head); return convertListToBST(0, this.values.size() - 1); } }
Appraoch #3: Python. [Inorder simualtion]
# Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = None # Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution: def findSize(self, head): ptr = head c = 0 while ptr: ptr = ptr.next c += 1 return c def sortedListToBST(self, head): """ :type head: ListNode :rtype: TreeNode """ size = self.findSize(head) def convert(l, r): nonlocal head if l > r: return None mid = (l + r) / 2 left = convert(l, mid-1) root = TreeNode(head.val) root.left = left head = head.next root.right = convert(mid+1, r) return root return convert(0, size-1)
永远渴望,大智若愚(stay hungry, stay foolish)