|
| 1 | +package binary_tree; |
| 2 | + |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +/** |
| 6 | + * Description: https://leetcode.com/problems/step-by-step-directions-from-a-binary-tree-node-to-another |
| 7 | + * Difficulty: Medium |
| 8 | + */ |
| 9 | +public class StepByStepDirectionsFromBinaryTreeNodeToAnother { |
| 10 | + |
| 11 | + /** |
| 12 | + * Time complexity: O(n) |
| 13 | + * Space complexity: O(n) |
| 14 | + */ |
| 15 | + public String getDirectionsViaBFS(TreeNode root, int startValue, int destValue) { |
| 16 | + Map<TreeNode, TreeNode> rootToParentMap = new HashMap<>(); |
| 17 | + findParents(root, rootToParentMap); |
| 18 | + |
| 19 | + // can find it while building parents map not to traverse the graph one more time |
| 20 | + TreeNode start = find(root, startValue); |
| 21 | + |
| 22 | + return findShortestPath(start, destValue, rootToParentMap); |
| 23 | + } |
| 24 | + |
| 25 | + private String findShortestPath(TreeNode start, int target, Map<TreeNode, TreeNode> rootToParentMap) { |
| 26 | + Queue<Pair> planned = new LinkedList<>(); |
| 27 | + Set<TreeNode> visited = new HashSet<>(); |
| 28 | + planned.offer(new Pair(start, "")); |
| 29 | + |
| 30 | + while (!planned.isEmpty()) { |
| 31 | + int levelSize = planned.size(); |
| 32 | + for (int i = 0; i < levelSize; i++) { |
| 33 | + Pair current = planned.poll(); |
| 34 | + TreeNode currentNode = current.node; |
| 35 | + if (currentNode.val == target) return current.path; |
| 36 | + |
| 37 | + if (currentNode.left != null && visited.add(currentNode.left)) { |
| 38 | + planned.offer(new Pair(currentNode.left, current.path + "L")); |
| 39 | + } |
| 40 | + |
| 41 | + if (currentNode.right != null && visited.add(currentNode.right)) { |
| 42 | + planned.offer(new Pair(currentNode.right, current.path + "R")); |
| 43 | + } |
| 44 | + |
| 45 | + TreeNode parent = rootToParentMap.get(currentNode); |
| 46 | + if (parent != null && visited.add(parent)) { |
| 47 | + planned.offer(new Pair(parent, current.path + "U")); |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + return ""; |
| 53 | + } |
| 54 | + |
| 55 | + private TreeNode find(TreeNode root, int val) { |
| 56 | + if (root == null) return null; |
| 57 | + if (root.val == val) return root; |
| 58 | + |
| 59 | + TreeNode left = find(root.left, val); |
| 60 | + TreeNode right = find(root.right, val); |
| 61 | + |
| 62 | + return left != null ? left : right; |
| 63 | + } |
| 64 | + |
| 65 | + private void findParents(TreeNode root, Map<TreeNode, TreeNode> parents) { |
| 66 | + if (root == null) return; |
| 67 | + |
| 68 | + if (root.left != null) { |
| 69 | + parents.put(root.left, root); |
| 70 | + findParents(root.left, parents); |
| 71 | + } |
| 72 | + |
| 73 | + if (root.right != null) { |
| 74 | + parents.put(root.right, root); |
| 75 | + findParents(root.right, parents); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + private record Pair(TreeNode node, String path) { |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Time complexity: O(n) |
| 84 | + * Space complexity: O(n) |
| 85 | + */ |
| 86 | + public String getDirectionsViaLCA(TreeNode root, int startValue, int destValue) { |
| 87 | + TreeNode lca = findLCA(root, startValue, destValue); |
| 88 | + String fromStart = buildPathFromStart(lca, startValue, new StringBuilder()); // start -> lca |
| 89 | + String toDest = buildPathToDest(lca, destValue, new StringBuilder()); // lca -> dest |
| 90 | + |
| 91 | + return fromStart + toDest; |
| 92 | + } |
| 93 | + |
| 94 | + private String buildPathFromStart(TreeNode root, int start, StringBuilder path) { |
| 95 | + if (root == null) return null; |
| 96 | + if (root.val == start) return path.toString(); |
| 97 | + |
| 98 | + path.append("U"); |
| 99 | + String left = buildPathFromStart(root.left, start, path); |
| 100 | + String right = buildPathFromStart(root.right, start, path); |
| 101 | + path.deleteCharAt(path.length() - 1); |
| 102 | + |
| 103 | + return left != null ? left : right; |
| 104 | + } |
| 105 | + |
| 106 | + private String buildPathToDest(TreeNode root, int dest, StringBuilder path) { |
| 107 | + if (root == null) return null; |
| 108 | + if (root.val == dest) return path.toString(); |
| 109 | + |
| 110 | + path.append("L"); |
| 111 | + String left = buildPathToDest(root.left, dest, path); |
| 112 | + path.deleteCharAt(path.length() - 1); |
| 113 | + |
| 114 | + path.append("R"); |
| 115 | + String right = buildPathToDest(root.right, dest, path); |
| 116 | + path.deleteCharAt(path.length() - 1); |
| 117 | + |
| 118 | + return left != null ? left : right; |
| 119 | + } |
| 120 | + |
| 121 | + private TreeNode findLCA(TreeNode root, int first, int second) { |
| 122 | + if (root == null) return null; |
| 123 | + if (root.val == first || root.val == second) return root; |
| 124 | + |
| 125 | + TreeNode left = findLCA(root.left, first, second); |
| 126 | + TreeNode right = findLCA(root.right, first, second); |
| 127 | + if (left == null) return right; |
| 128 | + if (right == null) return left; |
| 129 | + |
| 130 | + return root; |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Time complexity: O(n) |
| 135 | + * Space complexity: O(n) |
| 136 | + */ |
| 137 | + public String getDirectionsViaTwoPaths(TreeNode root, int startValue, int destValue) { |
| 138 | + String pathToStart = buildPath(root, startValue, new StringBuilder()); |
| 139 | + String pathToDest = buildPath(root, destValue, new StringBuilder()); |
| 140 | + |
| 141 | + // skip the common part of two paths |
| 142 | + int skip = 0; |
| 143 | + while (skip < pathToStart.length() |
| 144 | + && skip < pathToDest.length() |
| 145 | + && pathToStart.charAt(skip) == pathToDest.charAt(skip)) { |
| 146 | + skip++; |
| 147 | + } |
| 148 | + |
| 149 | + // replace all the pathToStart directions with 'U' |
| 150 | + StringBuilder directions = new StringBuilder(); |
| 151 | + for (int j = skip; j < pathToStart.length(); j++) { |
| 152 | + directions.append("U"); |
| 153 | + } |
| 154 | + |
| 155 | + return directions.append(pathToDest.substring(skip)).toString(); |
| 156 | + } |
| 157 | + |
| 158 | + private String buildPath(TreeNode root, int target, StringBuilder path) { |
| 159 | + if (root == null) return null; |
| 160 | + if (root.val == target) return path.toString(); |
| 161 | + |
| 162 | + path.append("L"); |
| 163 | + String left = buildPath(root.left, target, path); |
| 164 | + path.deleteCharAt(path.length() - 1); |
| 165 | + |
| 166 | + path.append("R"); |
| 167 | + String right = buildPath(root.right, target, path); |
| 168 | + path.deleteCharAt(path.length() - 1); |
| 169 | + |
| 170 | + return left != null ? left : right; |
| 171 | + } |
| 172 | + |
| 173 | + private static class TreeNode { |
| 174 | + int val; |
| 175 | + TreeNode left; |
| 176 | + TreeNode right; |
| 177 | + } |
| 178 | +} |
0 commit comments