I've been searching the web and can't find an answer to this. How can you find the leftmost/rightmost nodes of a tree with an arbitrary number of child nodes per node?

In a binary search tree this would presumably be the smallest and the greatest values in the tree. How would this work when any node can have any number of child nodes?

1 Answers

0
Community On

Only solution that comes to mind is something like, in JS syntax:

const getLeftmost = tree => tree.children ? getLeftmost(tree.children[0]) : tree

and about the same for rightmost. Is simply stepping down through the tree not enough? What language/structure are you exactly using?