Integer binary tree with depth and max
Question
Implement a binary tree of integers. How should an empty tree be represented? Write functions returning the tree's depth and its largest element.
Short answer
Nodes hold a value and two child pointers; the empty tree is a null pointer. Depth is recursive: for empty, otherwise of the children's depths. The maximum is the larger of the node value and the children's maxima, but it is undefined for an empty tree, so throw, return an optional or require non-empty input. Both are time and stack; manage node memory with smart pointers.
Adapted from Joshi, Denson & Downes, Quant Job Interview Questions and Answers, Q7.30, p.233; question and answer rewritten by SelfMadeQuant.com.
See the hint and the full worked solution in the question bank (free account).
Open the worked solution