-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_2689.java
50 lines (43 loc) · 1.36 KB
/
_2689.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
package com.fishercoder.solutions.thirdthousand;
public class _2689 {
// Definition for a rope tree node.
public static class RopeTreeNode {
public int len;
public String val;
public RopeTreeNode left;
public RopeTreeNode right;
public RopeTreeNode() {}
public RopeTreeNode(String val) {
this.len = 0;
this.val = val;
}
public RopeTreeNode(int len) {
this.len = len;
this.val = "";
}
public RopeTreeNode(int len, RopeTreeNode left, RopeTreeNode right) {
this.len = len;
this.val = "";
this.left = left;
this.right = right;
}
}
public static class Solution1 {
/*
* My completely original solution.
*/
public char getKthCharacter(RopeTreeNode root, int k) {
StringBuilder sb = new StringBuilder();
postOrderToConcatenate(root, k, sb);
return sb.charAt(k - 1);
}
private void postOrderToConcatenate(RopeTreeNode root, int k, StringBuilder sb) {
if (sb.length() >= k || root == null) {
return;
}
postOrderToConcatenate(root.left, k, sb);
postOrderToConcatenate(root.right, k, sb);
sb.append(root.val);
}
}
}