-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvert_array&list_bst.java
More file actions
77 lines (76 loc) · 2.27 KB
/
convert_array&list_bst.java
File metadata and controls
77 lines (76 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode sortedArrayToBST(int[] num) {
// Start typing your Java solution below
// DO NOT write main() function
//if(num.length==0) return null;
return buildBST(num,0,num.length-1);
}
public TreeNode buildBST(int[]num,int start,int end) {
if(start>end) return null;
int mid= start + (end - start)/2;//(start+end)/2;
TreeNode root=new TreeNode(num[mid]);
root.left=buildBST(num,start,mid-1);
root.right=buildBST(num,mid+1,end);
return root;
}
}
public class Solution {
ListNode p;
public TreeNode sortedListToBST(ListNode head) {
// Start typing your Java solution below
// DO NOT write main() function
p=head;
if(head==null) return null;
int len=0;
while(head!=null) {len++;head=head.next;}
return s(0,len-1);
}
public TreeNode s(int l,int h) {
if(l>h) return null;
int mid=l+(h-l)/2;
TreeNode left=s(l,mid-1);
TreeNode root=new TreeNode(p.val);
p=p.next;
root.left=left;
root.right=s(mid+1,h);
return root;
}
}
http://leetcode.com/2010/11/convert-sorted-list-to-balanced-binary.html
http://leetcode.com/2010/11/convert-binary-search-tree-bst-to.html#comment-72591
public class Solution {
public static ListNode head2;
public TreeNode sortedListToBST(ListNode head) {
// Start typing your Java solution below
// DO NOT write main() function
ListNode p=head;
head2=head;
int len=0;
while(p!=null) {
len++; p=p.next;
}
if(len==0) return null;
//private static ListNode h=new ListNode(1);h=head;
return buildBST(0,len-1);
}
public TreeNode buildBST(int start,int end) {
if(start>end) return null;
int mid=(start+end)/2;
TreeNode root=new TreeNode(0);
root.left=buildBST(start,mid-1);
//TreeNode root=new TreeNode(head2.val);
root.val=head2.val;
head2=head2.next;
root.right=buildBST(mid+1,end);
return root;
}
}