【LeetCode】Convert Sorted List to Binary Search Tree
題目:Convert Sorted List to Binary Search Tree
<span style="font-size:18px;">/**LeetCode convert link-list to Binary Search Tree
*題意:給定一個按照升序排列的連結串列,要求將其轉化為一個高度平衡的二查搜尋樹
*思路:因為連結串列是升序排列的,所以轉化為二叉搜尋樹是簡單的,但是要求是平衡的,即左右子數的高度 差在1之內
*可以取中間的點為根節點,保證兩邊數量是一樣的,這樣遞迴下來就能得到平衡的二叉樹
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; next = null; }
* }
*/
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
package javaTrain;
public class Train19 {
public TreeNode sortedListToBST(ListNode head) {
if(head == null) return null;
ListNode pre = head; //用於找到中間的節點
ListNode last = head;
TreeNode root;;
if(head.next == null){
root = new TreeNode(head.val);
root.left = null;
root.right = null;
return root;
}
while(pre != null && pre.next != null){ //找到中間節點
pre = pre.next.next;
last = last.next;
}
root = new TreeNode(last.val);
ListNode newHead = last.next;
last.next = null;
root.left = sortedListToBST(head);
root.right = sortedListToBST(newHead);
return root;
}
}
</span>
相關文章
- Leetcode Convert Sorted List to Binary Search TreeLeetCode
- Convert Sorted List to Binary Search Tree leetcode javaLeetCodeJava
- Java for LeetCode 109 Convert Sorted List to Binary Search TreeJavaLeetCode
- [LeetCode] 109. Convert Sorted List to Binary Search TreeLeetCode
- 【Leetcode】109. Convert Sorted List to Binary Search TreeLeetCode
- Leetcode Convert Sorted Array to Binary Search TreeLeetCode
- [leetcode]convert-sorted-array-to-binary-search-treeLeetCode
- Convert Sorted Array to Binary Search Tree leetcode javaLeetCodeJava
- LeetCode解題報告 108. Convert Sorted Array to Binary Search Tree [medium]LeetCode
- Leetcode Validate Binary Search TreeLeetCode
- leetcode 之 Recover Binary Search TreeLeetCode
- Recover Binary Search Tree leetcode javaLeetCodeJava
- Leetcode-Convert Sorted List to BST.LeetCode
- LeetCode-Closest Binary Search Tree ValueLeetCode
- Validate Binary Search Tree leetcode javaLeetCodeJava
- LeetCode 98. Validate Binary Search TreeLeetCode
- LeetCode-Closest Binary Search Tree Value IILeetCode
- 【LeetCode】Flatten Binary Tree to Linked ListLeetCode
- Leetcode Flatten Binary Tree to Linked ListLeetCode
- [LeetCode] 501. Find Mode in Binary Search TreeLeetCode
- LeetCode 501. Find Mode in Binary Search TreeLeetCode
- Leetcode-Flatten Binary Tree to Linked ListLeetCode
- Flatten Binary Tree to Linked List leetcode javaLeetCodeJava
- 173. Binary Search Tree Iterator
- LintCode-Search Range in Binary Search Tree
- Leetcode Search in Rotated Sorted ArrayLeetCode
- 501-Find Mode in Binary Search Tree
- Leetcode 33 Search in Rotated Sorted ArrayLeetCode
- Leetcode Search in Rotated Sorted Array IILeetCode
- Leetcode-Search in Rotated Sorted ArrayLeetCode
- Search in Rotated Sorted Array leetcode javaLeetCodeJava
- Leetcode-Convert Sorted Array to BSTLeetCode
- LintCode-Implement Iterator of Binary Search Tree
- LintCode-Remove node in Binary Search TreeREM
- Leetcode Binary Tree PathsLeetCode
- LeetCode Invert Binary TreeLeetCode
- Leetcode Balanced Binary TreeLeetCode
- Leetcode Unique Binary Search TreesLeetCode