# How to solve Leetcode 104. Maximum Depth of Binary Tree

## Problem statement

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

A binary tree's maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

### Example 1

![104_tmp-tree.jpg](https://cdn.hashnode.com/res/hashnode/image/upload/v1655500287594/2N84VW8rc.jpg align="left")

```plaintext
Input: root = [3,9,20,null,null,15,7]
Output: 3
```

### Example 2

```plaintext
Input: root = [1,null,2]
Output: 2
```

### Constraints

* The number of nodes in the tree is in the range `[0, 10^4]`.
    
* `-100 <= Node.val <= 100`.
    

## Solution

You have the following recursive relationship between the `root` and its children.

```plaintext
maxDepth(root) = max(maxDepth(root->left), maxDepth(root->right))
```

### Code

```cpp
#include <iostream>
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) {}
};

int maxDepth(TreeNode* root) {
    if (root == nullptr) {
        return 0;
    }
    return 1 + std::max(maxDepth(root->left), maxDepth(root->right));
}

int main() {
    TreeNode fifteen(15);
    TreeNode seven(7);
    TreeNode twenty(20, &fifteen, &seven);
    TreeNode nine(9);
    TreeNode three(3, &nine, &twenty);
    std::cout << maxDepth(&three) << std::endl;
    TreeNode two(2);
    TreeNode one(1, nullptr, &two);
    std::cout << maxDepth(&one) << std::endl;
}
```

```plaintext
Output:
3
2
```

### Complexity

* Runtime: `O(N)`, where `N` is the number of nodes.
    
* Extra space: `O(N)`.
    

## References

* [https://leetcode.com/problems/maximum-depth-of-binary-tree/](https://leetcode.com/problems/maximum-depth-of-binary-tree/)
    
* [https://www.leetsolve.com/104-maximum-depth-of-binary-tree](https://www.leetsolve.com/104-maximum-depth-of-binary-tree)
    

[Get my FREE book "10 Classic Coding Challenges"](https://nhutnguyen.gumroad.com/l/10_classic)
