You are given 2 Strings of equal length.
b <= a => g
c <= b => .
d <= c => f
e <= d => .
. <= e => .
. <= f => .
. <= g => .
y <= z => .
x <= y => .
w <= x => .
. <= w => .
class Solution {
public void populateHelper(String s1, String s2, HashMap<Character, Node> map, HashMap<Character, Boolean> marker,
int index) {
if (index == s1.length())
return;
Node root = map.getOrDefault(s1.charAt(index), new Node(s1.charAt(index)));
if (!marker.containsKey(s1.charAt(index)))
marker.put(s1.charAt(index), true);
Node child = map.getOrDefault(s2.charAt(index), new Node(s2.charAt(index)));
marker.put(s2.charAt(index), false);
if (root.left == null)
root.left = child;
else
root.right = child;
map.put(s1.charAt(index), root);
map.put(s2.charAt(index), child);
populateHelper(s1, s2, map, marker, index + 1);
}
public ArrayList<Node> populate(String str1, String str2) {
HashMap<Character, Node> map = new HashMap<>();
HashMap<Character, Boolean> marker = new HashMap<>();
populateHelper(str1, str2, map, marker, 0);
ArrayList<Node> ans = new ArrayList<>();
for (char x : marker.keySet())
if (marker.get(x))
ans.add(map.get(x));
return ans;
}
}