ATM analogy for leetcode #104 Maximum Depth of Binary Tree

Andrei Kruglov
Jul 24, 2021

--

I’ve found an ATM analogy answering what recursion is in this video.

This analogy may help you to solve leetcode #104 Maximum Depth of Binary Tree. All you need is to imagine that you have multiple ATMs and need to choose a max path:

public int MaxDepth(TreeNode root)
{
if (root == null)
return 0;

var left = 1 + MaxDepth(root.left);
var right = 1 + MaxDepth(root.right);

return Math.Max(left, right);
}

As for me, it’s a simple analogy helping to remember the recursive approach for solving this problem.

--

--

No responses yet