Skip to content

How do you find the middle element of a linked list? #1679

Answered by Emojees
Kuefo asked this question in Q&A
Discussion options

You must be logged in to vote

Approach:

The idea is to traverse the tree once and get the minimum and maximum horizontal distance with respect to root. For the tree shown above, minimum distance is -2 (for node with value 4) and maximum distance is 3 (For node with value 9).
Once we have maximum and minimum distances from root, we iterate for each vertical line at distance minimum to maximum from root, and for each vertical line traverse the tree and print the nodes which lie on that vertical line.

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by Kuefo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants