-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution988.cs
41 lines (32 loc) · 974 Bytes
/
Solution988.cs
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
using System.Text;
using LeetCode.DataStructures;
namespace LeetCode.Solutions;
public class Solution988
{
/// <summary>
/// 988. Smallest String Starting From Leaf - Medium
/// <a href="https://leetcode.com/problems/smallest-string-starting-from-leaf">See the problem</a>
/// </summary>
public string SmallestFromLeaf(TreeNode root)
{
var sb = new StringBuilder();
var result = new List<string>();
void Dfs(TreeNode node, StringBuilder path)
{
if (node == null)
{
return;
}
path.Insert(0, (char)('a' + node.val));
if (node.left == null && node.right == null)
{
result.Add(path.ToString());
}
Dfs(node.left, path);
Dfs(node.right, path);
path.Remove(0, 1);
}
Dfs(root, sb);
return result.OrderBy(x => x).FirstOrDefault();
}
}