Skip to content

Instantly share code, notes, and snippets.

@MelulekiDube
Created October 13, 2018 07:30
Show Gist options
  • Save MelulekiDube/92c6d4311d0a3cdbef11dc9a61a07065 to your computer and use it in GitHub Desktop.
Save MelulekiDube/92c6d4311d0a3cdbef11dc9a61a07065 to your computer and use it in GitHub Desktop.
public void topView(Node node, int level, int distance, Map<Integer, Pair<Integer, T>> map) {
if (node == null) {
return;
}
//check if the map already has an entry of this level, and if the level of that entry was the minimum
if (!map.containsKey(distance) || (map.containsKey(distance) && level < map.get(distance).getKey()/*this is the level*/)) {
// we need to update the values in that case:
map.put(distance, new Pair<>(level, (T) node.data));// we put the new level distance relation we just came through
}
topView(node.leftChild, distance - 1, level + 1, map);
topView(node.rightChild, distance + 1, level + 1, map);
}
@MelulekiDube
Copy link
Author

I tried debugging it and as you recurse to the left of the left child the distance then is 0 I don't know how coz if I have -1 -1 it should ideally give me -2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment