leetcode--ConvertSortedListtoBinarySearchTree
2015-06-23 10:14
393 查看
思路:如果用数组可以直接取到中间值,因此可以用前序遍历的顺序来构建BST。但是链表如果要取中间值需要n/2的时间,因此采用中序遍历的顺序来构建BST。 这样root生成的顺序就和中序遍历的顺序一样,也就是list从前往后的顺序。
import java.util.List;
/**
* Created by marsares on 15/6/23.
*/
public class ConvertSortedListtoBinarySearchTree {
ListNode ln;
public TreeNode sortedListToBST(ListNode head) {
ln=head;
return traversal(1,getLen(head));
}
public TreeNode traversal(int start,int end){
if(start>end)return null;
int mid=(start+end)/2;
TreeNode left=traversal(start,mid-1);
TreeNode root=new TreeNode(ln.val);
ln=ln.next;
TreeNode right=traversal(mid+1,end);
root.left=left;
root.right=right;
return root;
}
public int getLen(ListNode head){
if(head==null)return 0;
int len=1;
while(head.next!=null){
len++;
head=head.next;
}
return len;
}
public static void main(String[]args){
ConvertSortedListtoBinarySearchTree csltobst=new ConvertSortedListtoBinarySearchTree();
BinaryTreeSerialize bts=new BinaryTreeSerialize();
ListNode n0=new ListNode(1);
ListNode n1=new ListNode(2);
ListNode n2=new ListNode(3);
ListNode n3=new ListNode(4);
ListNode n4=new ListNode(5);
n0.next=n1;
n1.next=n2;
n2.next=n3;
n3.next=n4;
System.out.println(bts.Serialize(csltobst.sortedListToBST(n0)));
}
}
import java.util.List;
/**
* Created by marsares on 15/6/23.
*/
public class ConvertSortedListtoBinarySearchTree {
ListNode ln;
public TreeNode sortedListToBST(ListNode head) {
ln=head;
return traversal(1,getLen(head));
}
public TreeNode traversal(int start,int end){
if(start>end)return null;
int mid=(start+end)/2;
TreeNode left=traversal(start,mid-1);
TreeNode root=new TreeNode(ln.val);
ln=ln.next;
TreeNode right=traversal(mid+1,end);
root.left=left;
root.right=right;
return root;
}
public int getLen(ListNode head){
if(head==null)return 0;
int len=1;
while(head.next!=null){
len++;
head=head.next;
}
return len;
}
public static void main(String[]args){
ConvertSortedListtoBinarySearchTree csltobst=new ConvertSortedListtoBinarySearchTree();
BinaryTreeSerialize bts=new BinaryTreeSerialize();
ListNode n0=new ListNode(1);
ListNode n1=new ListNode(2);
ListNode n2=new ListNode(3);
ListNode n3=new ListNode(4);
ListNode n4=new ListNode(5);
n0.next=n1;
n1.next=n2;
n2.next=n3;
n3.next=n4;
System.out.println(bts.Serialize(csltobst.sortedListToBST(n0)));
}
}
相关文章推荐
- leetcode 179 Largest Number
- leetcode 24 Swap Nodes in Pairs
- leetcode 2 Add Two Numbers 方法1
- leetcode 2 Add Two Numbers 方法2
- [LeetCode]47 Permutations II
- [LeetCode]65 Valid Number
- [LeetCode]123 Best Time to Buy and Sell Stock III
- [LeetCode] String Reorder Distance Apart
- [LeetCode] Sliding Window Maximum
- [LeetCode] Find the k-th Smallest Element in the Union of Two Sorted Arrays
- [LeetCode] Determine If Two Rectangles Overlap
- [LeetCode] A Distance Maximizing Problem
- LeetCode[Day 1] Two Sum 题解
- LeetCode[Day 2] Median of Two Sorted Arrays 题解
- LeetCode[Day 3] Longest Substring Without... 题解
- LeetCode [Day 4] Add Two Numbers 题解
- LeetCode [Day 5] Longest Palindromic Substring 题解
- LeetCode [Day 6] ZigZag Conversion 题解
- LeetCode 2: Add Two Numbers (JAVA)
- LeetCode 1: Two Sum (JAVA)