-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSmallestStringStartingFromLeaf.java
More file actions
47 lines (35 loc) · 1.51 KB
/
Copy pathSmallestStringStartingFromLeaf.java
File metadata and controls
47 lines (35 loc) · 1.51 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
// Find the lexicographically smallest string
// that starts at a leaf of this tree and ends at the root.
// See: https://leetcode.com/problems/smallest-string-starting-from-leaf/
package leetcode.tree;
import static leetcode.util.tree.BinTreeUtil.initTree;
import leetcode.util.tree.TreeNode;
public class SmallestStringStartingFromLeaf {
private String ans = "{";
public String smallestFromLeaf(TreeNode root) {
helper(root, "");
return ans;
}
public void helper(TreeNode root, String curr) {
if (root == null)
return;
if (root.left == null && root.right == null) {
String candidate = (char) (root.val + 97) + curr;
if (candidate.compareTo(ans) < 0) {
ans = candidate;
}
}
helper(root.left, (char) (root.val + 97) + curr);
helper(root.right, (char) (root.val + 97) + curr);
}
public static void main(String[] args) {
System.out.println(new SmallestStringStartingFromLeaf()
.smallestFromLeaf(initTree(0, 1, 2, 3, 4, 3, 4)));
System.out.println(new SmallestStringStartingFromLeaf()
.smallestFromLeaf(initTree(25, 1, 3, 1, 3, 0, 2)));
System.out.println(new SmallestStringStartingFromLeaf()
.smallestFromLeaf(initTree(2, 2, 1, null, 1, 0, null, 0)));
System.out.println(new SmallestStringStartingFromLeaf().
smallestFromLeaf(initTree(3, 9, 20, null, null, 15, 7)));
}
}