Skip to content

Commit 0b97eeb

Browse files
committed
Runtime: 35 ms (Top 23.02%) | Memory: 47.3 MB (Top 32.61%)
1 parent f7db675 commit 0b97eeb

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
// Runtime: 35 ms (Top 23.02%) | Memory: 47.3 MB (Top 32.61%)
12
class CBTInserter {
2-
3+
34
private TreeNode root;
45
private int total;
5-
6+
67
private int count(TreeNode root) {
78
if (root == null) return 0;
89
return 1+count(root.left)+count(root.right);
@@ -12,7 +13,7 @@ public CBTInserter(TreeNode root) {
1213
this.root = root;
1314
total = count(root);
1415
}
15-
16+
1617
private int insertBinary(int val, int k, int right) {
1718
int left = 0;
1819
var ptr = root;
@@ -27,13 +28,13 @@ private int insertBinary(int val, int k, int right) {
2728
ptr = ptr.left;
2829
right = mid;
2930
} else if (mid < k) {
30-
left = mid+1;
31+
left = mid+1;
3132
ptr = ptr.right;
3233
}
3334
}
3435
return 0;
3536
}
36-
37+
3738
public int insert(int val) {
3839
int depth = 0;
3940
int n = total;
@@ -42,19 +43,19 @@ public int insert(int val) {
4243
n /= 2;
4344
}
4445
if ((1<<depth)-1 == total) depth++;
45-
// k is the new index in the lowest level of tree, right is the max index of the lowest level of tree
46-
// e.g. for tree
47-
// 1
48-
// / \
49-
// 2 new
50-
//index 0 1
51-
// it's insertBinary(val, 1, 1)
46+
// k is the new index in the lowest level of tree, right is the max index of the lowest level of tree
47+
// e.g. for tree
48+
// 1
49+
// / \
50+
// 2 new
51+
//index 0 1
52+
// it's insertBinary(val, 1, 1)
5253
var res = insertBinary(val, total - (1<<(depth-1))+1 , (1<<(depth-1)) -1);
5354
total++;
5455
return res;
5556
}
56-
57+
5758
public TreeNode get_root() {
5859
return root;
5960
}
60-
}
61+
}

0 commit comments

Comments
 (0)