---
# System prepended metadata

title: Maximum Depth of Binary Tree
tags: [Tree, Easy]

---

# Maximum Depth of Binary Tree
###### tags: `Easy`、`Tree`

![](https://i.imgur.com/DiPE25r.png)

## 遞迴解
- 想法:
```python=
# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution(object):
    def maxDepth(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """
        if root is None:
            return 0
        return 1 + max(self.maxDepth(root.right),self.maxDepth(root.left))
```

## 非遞迴解
- 想法:
```python=
# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution(object):
    def maxDepth(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """
        if not root:
            return 0
        
        root = [root]
        level = 0
        
        while root:
            temp = []
            level += 1
            
            for node in root:
                if node.left:
                    temp.append(node.left)
                if node.right:
                    temp.append(node.right)
            root = temp
        return level
```
