## Solve Tree Problems Recursively

- “Top-down” Solution
- “Bottom-up” Solution

Conclusion

In previous sections, we have introduced how to solve tree traversal problem recursively. Recursion is one of the most powerful and frequent used methods for solving tree related problems.

As we know, a tree can be defined recursively as a node(the root node), which includes a value and a list of references to other nodes. Recursion is one of the nature features of a tree. Therefore, many tree problems can be solved recursively. For each recursion level, we can only focus on the problem within one single node and call the function recursively to solve its children.

Typically, we can solve a tree problem recursively from the top down or from the bottom up.

### “Top-down” Solution

“Top-down” means that in each recursion level, we will visit the node first to come up with some values, and pass these values to its children when calling the function recursively. So the “top-down” solution can be considered as kind of preorder traversal. To be specific, the recursion function top_down(root, params) works like this:

1 | 1. return specific value for null node |

We know that the depth of the root node is 1. For each node, if we know the depth of the node, we will know the depth of its children. Therefore, if we pass the depth of the node as a parameter when calling the function recursively, all the nodes know the depth of themselves. And for leaf nodes, we can use the depth to update the final answer. Here is the pseudocode for the recursion function maximum_depth(root, depth):

1 | 1. return if root is null |

### “Bottom-up” Solution

“Bottom-up” is another recursion solution. In each recursion level, we will firstly call the functions recursively for all the children nodes and then come up with the answer according to the return values and the value of the root node itself. This process can be regarded as kind of postorder traversal. Typically, a “bottom-up” recursion function bottom_up(root) will be like this:

1 | 1. return specific value for null node |

Let’s go on discussing the question about maximum depth but using a different way of thinking: for a single node of the tree, what will be the maximum depth x of the subtree rooted at itself?

If we know the maximum depth l of the subtree rooted at its left child and the maximum depth r of the subtree rooted at its right child, can we answer the previous question? Of course yes, we can choose the maximum between them and plus 1 to get the maximum depth of the subtree rooted at the selected node. That is x = max(l, r) + 1.

It means that for each node, we can get the answer after solving the problem of its children. Therefore, we can solve this problem using a “bottom-up” solution. Here is the pseudocode for the recursion function maximum_depth(root):

1 | 1. return 0 if root is null // return 0 for null node |