-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSmallestStrStartingFromLeaf988.java
56 lines (44 loc) · 1.93 KB
/
SmallestStrStartingFromLeaf988.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
public class SmallestStrStartingFromLeaf988 {
/*
* You are given the root of a binary tree where each node has a value in the range [0, 25] representing the letters 'a' to 'z'.
Return the lexicographically smallest string that starts at a leaf of this tree and ends at the root.
As a reminder, any shorter prefix of a string is lexicographically smaller.
For example, "ab" is lexicographically smaller than "aba".
A leaf of a node is a node that has no children.
*/
// Definition for a binary tree node.
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode() {}
TreeNode(int val) { this.val = val; }
TreeNode(int val, TreeNode left, TreeNode right) {
this.val = val;
this.left = left;
this.right = right;
}
}
class Solution {
public String smallestFromLeaf(TreeNode root) {
StringBuilder smallest = new StringBuilder();
dfs(root, new StringBuilder(), smallest);
return smallest.toString();
}
private void dfs(TreeNode node, StringBuilder path, StringBuilder smallest) {
if (node == null) return;
path.append((char)('a' + node.val));
if (node.left == null && node.right == null) {
String currentString = path.reverse().toString();
if (smallest.length() == 0 || currentString.compareTo(smallest.toString()) < 0) {
smallest.setLength(0);
smallest.append(currentString);
}
path.reverse();
}
dfs(node.left, path, smallest);
dfs(node.right, path, smallest);
path.setLength(path.length() - 1);
}
}
}