> For the complete documentation index, see [llms.txt](https://timmybeeflin.gitbook.io/cracking-leetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://timmybeeflin.gitbook.io/cracking-leetcode/tree/104.-maximum-depth-of-binary-tree.md).

# 104. Maximum Depth of Binary Tree (divide & conquer)

{% embed url="<https://leetcode.com/problems/maximum-depth-of-binary-tree/>" %}

time: O(n), visit each node exactly once

space: O(n), call stack worst case is n (depth n)

### compare to no. 111

this is postorder

```java
class Solution {
    public int maxDepth(TreeNode root) {
        if (root == null) {
            return 0;
        }

        int left = maxDepth(root.right);
        int right = maxDepth(root.left);

        return Math.max(left, right) + 1;
    }
}
```

or like this

```java
class Solution {
    public int maxDepth(TreeNode root) {
        if (root == null) return 0;
        return 1 + Math.max(maxDepth(root.left), maxDepth(root.right));
    }
}
```
