Leetcode-Convert Sorted List to BST.

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

Solution:

 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 tree
11  * 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)
21             return null;
22
23         ListNode end = head;
24         while (end.next!=null){
25             end = end.next;
26         }
27         TreeNode root = sortedListToBSTRecur(head,end);
28
29         return root;
30     }
31
32     //Convert the list from head to end into BST, return the root node
33     public TreeNode sortedListToBSTRecur(ListNode head, ListNode end){
34         //If only one node.
35         if (head==end){
36             TreeNode root = new TreeNode(head.val);
37             return root;
38         }
39
40         //If only two node.
41         if (head.next==end){
42             TreeNode root = new TreeNode(head.val);
43             TreeNode child = new TreeNode(end.val);
44             root.right = child;
45             return root;
46         }
47
48
49         //Otherwise, count the number of node in the list, find out the median one,
50         //and convert the left list and right list to BST.
51         int nodeNum = 1;
52         ListNode curNode = head;
53         while (curNode!=end){
54             curNode = curNode.next;
55             nodeNum++;
56         }
57         int mid = nodeNum/2+nodeNum%2;
58         ListNode median = null;
59         ListNode pre = null;
60         curNode = head;
61         //NOTE: we only need to move (mid-1) steps to move curNode to the median one.
62         for (int i=0;i<mid-1;i++){
63             pre = curNode;
64             curNode = curNode.next;
65         }
66         median = curNode;
67         TreeNode root = new TreeNode(median.val);
68         TreeNode leftChild = sortedListToBSTRecur(head,pre);
69         TreeNode rightChild = sortedListToBSTRecur(median.next,end);
70         root.left = leftChild;
71         root.right = rightChild;
72         return root;
73     }
74 }

For a list from head to end, we find out the median node and use recursion method to convert its left list and right list to BST, and then connect the left subtree and right subtree to the median node.

时间: 2024-10-14 14:24:42

Leetcode-Convert Sorted List to BST.的相关文章

LeetCode: Convert Sorted List to Binary Search Tree [109]

[题目] Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. [题意] 将一个有序链表转换成平衡二叉树 [思路] 思路跟Convert Sorted Array to Binary Search Tree完全一样 [代码] /** * Definition for singly-linked list. * struct List

[LeetCode] 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. 方法:为了使BST高度平衡,要找链表中的中值作为当前根节点. /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) :

LeetCode: Convert Sorted Array to Binary Search Tree [108]

[题目] Given an array where elements are sorted in ascending order, convert it to a height balanced BST. [题意] 给定一个已排序的数组(不存在重复元素),将它转换成一棵平衡二叉搜索树. [思路] 由于平衡二叉树要求左右子树的高度差绝对值相遇等于1,也就是说左右子树尽可能包含相同数目节点. 则使用二分法来解本题即可. [代码] /** * Definition for binary tree *

[leetcode]Convert Sorted List to Binary Search Tree @ Python

原题地址:http://oj.leetcode.com/problems/convert-sorted-list-to-binary-search-tree/ 题意:将一条排序好的链表转换为二叉查找树,二叉查找树需要平衡. 解题思路:两个思路:一,可以使用快慢指针来找到中间的那个节点,然后将这个节点作为树根,并分别递归这个节点左右两边的链表产生左右子树,这样的好处是不需要使用额外的空间,坏处是代码不够整洁.二,将排序好的链表的每个节点的值存入一个数组中,这样就和http://www.cnblog

[leetcode]Convert Sorted Array to Binary Search Tree @ Python

原题地址:http://oj.leetcode.com/problems/convert-sorted-array-to-binary-search-tree/ 题意:将一个排序好的数组转换为一颗二叉查找树,这颗二叉查找树要求是平衡的. 解题思路:由于要求二叉查找树是平衡的.所以我们可以选在数组的中间那个数当树根root,然后这个数左边的数组为左子树,右边的数组为右子树,分别递归产生左右子树就可以了. 代码: # Definition for a binary tree node # class

109.Convert sorted list to BST

/* * 109.Convert sorted list to BST * 2016.12.24 by Mingyang * 这里的问题是对于一个链表我们是不能常量时间访问它的中间元素的. * 这时候就要利用到树的中序遍历了,按照递归中序遍历的顺序对链表结点一个个进行访问,而我们要构造的二分查找树正是按照链表的顺序来的. * 思路就是先对左子树进行递归 * 然后将当前结点作为根,迭代到下一个链表结点,最后在递归求出右子树即可.整体过程就是一次中序遍历,时间复杂度是O(n),空间复杂度是栈空间O(

[LeetCode] 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. 这道题是要求把有序链表转为二叉搜索树,和之前那道Convert Sorted Array to Binary Search Tree 将有序数组转为二叉搜索树思路完全一样,只不过是操作的数据类型有所差别,一个是数组,一个是链表.数组方便就方便在可以通过index直接访问任意一个元

LeetCode: Convert Sorted Array to Binary Search Tree 解题报告

Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascending order, convert it to a height balanced BST.SOLUTION 1:使用递归解决. base case: 当索引值超过时,返回null. 递归主体:构造根节点,调用递归构造左右子树.并将左右子树挂在根节点上. 返回值:返回构造的根节点. GITHUB: https:

LeetCode——Convert Sorted Array to Binary Search Tree

Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 原题链接:https://oj.leetcode.com/problems/convert-sorted-array-to-binary-search-tree/ 题目:给定一个升序排列元素的数组,将其转换成高度平衡的二叉树. 思路:已经排序.则从中间劈开,中间元素为树的根,左右递归构建. public

LeetCode——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. 原题链接:https://oj.leetcode.com/problems/convert-sorted-list-to-binary-search-tree/ 题目:给定一个有正序链表,将其转换成一个二叉搜索树. 思路:能够依照之前的数组的思路来做.即找到中间值.再左右递归建树