Determine max and min root leaf path in a Binary Search Tree (BST)

Let’s view an algorithm that manage to find the minimum and the maximum root-leaf path in a binary search tree. I used three stacks in this algorithm, one is temporary and the other will be filled with the results. Note that if we change:

  • i > maxSeq.size() with i >= maxSeq.size() we get the longest root-leaf path from the right instead of from the left
  • i with i we get the shortest root-leaf path from the left instead of from the right