rav16783

Untitled

Mar 21st, 2023
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. The approach is to use the preorder traversal of the tree to traverse the tree and check that we have visited the current vertical level and if visited then we can check for the smaller horizontal level node and store it. Else we can just update the vertical level as visited and store the node and horizontal level of the current node.
  2. • We have to create the map to check whether the horizontal level is visited or not as it will state the horizontal distance of the node from the root node. Where key will represent the horizontal distance and the value is the pair containing the value and level of each node.
  3. • We will traverse the tree using the preorder traversal.
  4. • Every time we check if the level of the current horizontal distance is less than the max level seen till now then we will update the value and the level for this horizontal distance.
  5. • We will pass level-1 for the left child and level+1 for the right child to get the vertical level.
  6. • Print the values present in the map.
Advertisement
Add Comment
Please, Sign In to add comment