Maximum Depth of Binary Tree

Problem

Given the root of a binary tree, return its depth.

The depth of a binary tree is defined as the number of nodes along the longest path from the root node down to the farthest leaf node.

Examples

Example 1:

Input: root = [1,2,3,null,null,4]

Output: 3

Example 2:

Input: root = []

Output: 0

Constraints

  • 0 <= The number of nodes in the tree <= 100.
  • -100 <= Node.val <= 100

You should aim for a solution with O(n) time and O(n) space, where n is the number of nodes in the tree.

Solution

Maximum depth is 1 + max depth of left/right subtrees

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
 
class Solution {
public:
    int maxDepth(TreeNode* root) {
        // Max depth is max(d(left), d(right)) + 1
        std::function<int(TreeNode*)> runner;
        runner = [&](TreeNode *node) -> int {
            // Base case
            if (!node) {
                return 0;
            }
            return std::max(runner(node->left), runner(node->right)) + 1;
        };
        return runner(root);
    }
};