-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainClass.java
97 lines (76 loc) · 2.78 KB
/
MainClass.java
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
Given an array 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.
*/
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
import java.util.Arrays;
class Solution {
public TreeNode sortedArrayToBST(int[] nums) {
if(nums.length == 0)
{
return null;
}
if(nums.length == 1)
{
TreeNode root = new TreeNode(nums[0]);
root.right=root.left=null;
return root;
}
int mid = nums.length/2;
System.out.println(mid);
TreeNode root =new TreeNode(nums[mid]);
root.left=sortedArrayToBST( Arrays.copyOfRange(nums, 0, mid));
root.right=sortedArrayToBST(Arrays.copyOfRange(nums, mid+1,nums.length));
return root;
}
}
public class MainClass {
public static int[] stringToIntegerArray(String input) {
input = input.trim();
input = input.substring(1, input.length() - 1);
if (input.length() == 0) {
return new int[0];
}
String[] parts = input.split(",");
int[] output = new int[parts.length];
for(int index = 0; index < parts.length; index++) {
String part = parts[index].trim();
output[index] = Integer.parseInt(part);
}
return output;
}
public static String treeNodeToString(TreeNode root) {
if (root == null) {
return "[]";
}
String output = "";
Queue<TreeNode> nodeQueue = new LinkedList<>();
nodeQueue.add(root);
while(!nodeQueue.isEmpty()) {
TreeNode node = nodeQueue.remove();
if (node == null) {
output += "null, ";
continue;
}
output += String.valueOf(node.val) + ", ";
nodeQueue.add(node.left);
nodeQueue.add(node.right);
}
return "[" + output.substring(0, output.length() - 2) + "]";
}
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String line;
while ((line = in.readLine()) != null) {
int[] nums = stringToIntegerArray(line);
TreeNode ret = new Solution().sortedArrayToBST(nums);
String out = treeNodeToString(ret);
System.out.print(out);
}
}
}