Created
October 13, 2018 07:30
-
-
Save MelulekiDube/92c6d4311d0a3cdbef11dc9a61a07065 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.