Top-Down Solution
Using the value that came up with the first node, pass the value to the children nodes. It can be considered as pre-order tree traversal.
If you can use parameters and the value of the node itself to determine what should be the parameters passed to its children, you should use “Top-Down” solution.
Example)

Bottom-Up Solution
Call the children nodes first and then using the returned values, come up with the current node, which is the parent.
If using the answer of children, you can calculate the answer of current node, you should be using “Bottom-Up” solution.
Example)

Let’s say maximum depth l as the subtree rooted at its left child and the maximum depth r of the subtree rooted at its right child.
To get the maximum depth x of the subtree rooted from current node, we can choose the maximum between them and add 1 to get the maximum depth of the subtree rooted at the current node. That is x = max(l, r) + 1.

Leave a comment