My attempt at one of Amazon's interview questions; also assignment 5.13.3 in "Data Structures and Algorithms in C++" (1st ed) by Adam Drozdek.