One testcase not working pls check
Your implementation is wrong.What if a particular distance occurs at lower level on right side.You will still print it as you are calling it after left and it will be the last one to be added.
You should pass level as well in the recusive function and for each d,check if it is not in map or current level is greater than what is in map,then update the value of m[d] to currentnode->val,current_level.
Ps:make map as pair.