|
| 1 | +package com.fishercoder.solutions; |
| 2 | + |
| 3 | +import com.fishercoder.common.classes.TreeNode; |
| 4 | + |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.Collections; |
| 7 | +import java.util.HashMap; |
| 8 | +import java.util.List; |
| 9 | +import java.util.Map; |
| 10 | + |
| 11 | +/** |
| 12 | + * 988. Smallest String Starting From Leaf |
| 13 | + * |
| 14 | + * Given the root of a binary tree, each node has a value from 0 to 25 representing the letters 'a' to 'z': |
| 15 | + * a value of 0 represents 'a', a value of 1 represents 'b', and so on. |
| 16 | + * Find the lexicographically smallest string that starts at a leaf of this tree and ends at the root. |
| 17 | + * (As a reminder, any shorter prefix of a string is lexicographically smaller: for example, "ab" is lexicographically smaller than "aba". |
| 18 | + * A leaf of a node is a node that has no children.) |
| 19 | + * |
| 20 | + * Example 1: |
| 21 | + * Input: [0,1,2,3,4,3,4] |
| 22 | + * Output: "dba" |
| 23 | + * |
| 24 | + * Example 2: |
| 25 | + * Input: [25,1,3,1,3,0,2] |
| 26 | + * Output: "adz" |
| 27 | + * |
| 28 | + * Example 3: |
| 29 | + * Input: [2,2,1,null,1,0,null,0] |
| 30 | + * Output: "abc" |
| 31 | + * |
| 32 | + * Note: |
| 33 | + * The number of nodes in the given tree will be between 1 and 8500. |
| 34 | + * Each node in the tree will have a value between 0 and 25. |
| 35 | + * */ |
| 36 | +public class _988 { |
| 37 | + public static class Solution1 { |
| 38 | + public String smallestFromLeaf(TreeNode root) { |
| 39 | + List<String> paths = new ArrayList<>(); |
| 40 | + Map<Integer, Character> map = new HashMap<>(); |
| 41 | + map.put(0, 'a'); |
| 42 | + map.put(1, 'b'); |
| 43 | + map.put(2, 'c'); |
| 44 | + map.put(3, 'd'); |
| 45 | + map.put(4, 'e'); |
| 46 | + map.put(5, 'f'); |
| 47 | + map.put(6, 'g'); |
| 48 | + map.put(7, 'h'); |
| 49 | + map.put(8, 'i'); |
| 50 | + map.put(9, 'j'); |
| 51 | + map.put(10, 'k'); |
| 52 | + map.put(11, 'l'); |
| 53 | + map.put(12, 'm'); |
| 54 | + map.put(13, 'n'); |
| 55 | + map.put(14, 'o'); |
| 56 | + map.put(15, 'p'); |
| 57 | + map.put(16, 'q'); |
| 58 | + map.put(17, 'r'); |
| 59 | + map.put(18, 's'); |
| 60 | + map.put(19, 't'); |
| 61 | + map.put(20, 'u'); |
| 62 | + map.put(21, 'v'); |
| 63 | + map.put(22, 'w'); |
| 64 | + map.put(23, 'x'); |
| 65 | + map.put(24, 'y'); |
| 66 | + map.put(25, 'z'); |
| 67 | + dfs(root, "", paths, map); |
| 68 | + return findSmallest(paths); |
| 69 | + } |
| 70 | + |
| 71 | + private String findSmallest(List<String> paths) { |
| 72 | + List<String> reversed = new ArrayList<>(); |
| 73 | + for (String path : paths) { |
| 74 | + StringBuilder sb = new StringBuilder(); |
| 75 | + sb.append(path); |
| 76 | + reversed.add(sb.reverse().toString()); |
| 77 | + } |
| 78 | + Collections.sort(reversed); |
| 79 | + return reversed.get(0); |
| 80 | + } |
| 81 | + |
| 82 | + private void dfs(TreeNode root, String path, List<String> paths, Map<Integer, Character> map) { |
| 83 | + if (root == null) { |
| 84 | + return; |
| 85 | + } |
| 86 | + path += map.get(root.val); |
| 87 | + if (root.left == null && root.right == null) { |
| 88 | + paths.add(path); |
| 89 | + } |
| 90 | + dfs(root.left, path, paths, map); |
| 91 | + dfs(root.right, path, paths, map); |
| 92 | + path = path.substring(0, path.length() - 1); |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments