I am trying to create one method that will find all nodes from the paths from the root to each leaf in a binary search tree and store them in an array.
So far I have made a rediculous method that works fine if the right side of the root doesn’t have more than one node that’s parent to two nodes. It took me a long time to figure out what was wrong, but if my current method is to work I have to know the tree, and that’s just stupid.
This is basically what I’m trying to do:

Output: [[8, 3, 1],[8 ,3 ,6 ,4],[8, 3, 6, 7],[8, 10, 14, 13]]
I want to avoid recursion and rather use stack. But I don’t see how I can "control" which nodes to pop from the stack.. What if they have subtrees with subtrees.
Something like this: