# Maximum Depth of Binary Tree

> Given a binary tree, find its maximum depth.

> The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

```c++
class Solution {
public:
int num;
int maxDepth(TreeNode *root) {
if(!root) {
return 0;
}

//首先初始化num为最小值
num = numeric_limits::min();
travel(root, 1);
return num;
}

void travel(TreeNode* node, int level) {
//如果没有左子树以及右子树了，就到了叶子节点
if(!node->left && !node->right) {
num = max(num, level);
return;
}

if(node->left) {
travel(node->left, level + 1);
}

if(node->right) {
travel(node->right, level + 1);
}
}
};
```

# Minimum Depth of Binary Tree

> Given a binary tree, find its minimum depth.

> The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.

```c++
class Solution {
public:
int n;
int minDepth(TreeNode *root) {
if(!root) {
return 0;
}

//初始化成最大值
n = numeric_limits::max();
int d = 1;

depth(root, d);
return n;
}

void depth(TreeNode* node, int& d) {
//叶子节点，比较
if(!node->left && !node->right) {
n = min(n, d);
return;
}

if(node->left) {
d++;
depth(node->left, d);
d--;
}

if(node->right) {
d++;
depth(node->right, d);
d--;
}
}
};
```