2022-07-05 09:31:16 +08:00
|
|
|
|
# Leetcode Maximum-Depth-Of-Binary-Tree
|
|
|
|
|
|
|
|
|
|
#### 2022-07-05 09:25
|
|
|
|
|
|
|
|
|
|
> ##### Algorithms:
|
|
|
|
|
> #algorithm #BFS
|
|
|
|
|
> ##### Data structures:
|
|
|
|
|
> #DS #binary_tree
|
|
|
|
|
> ##### Difficulty:
|
|
|
|
|
> #coding_problem #difficulty-easy
|
|
|
|
|
> ##### Additional tags:
|
|
|
|
|
> #leetcode
|
|
|
|
|
> ##### Revisions:
|
|
|
|
|
> N/A
|
|
|
|
|
|
|
|
|
|
##### Related topics:
|
|
|
|
|
```expander
|
|
|
|
|
tag:#BFS
|
|
|
|
|
```
|
|
|
|
|
|
2022-07-05 10:48:41 +08:00
|
|
|
|
- [[Leetcode Binary-Tree-Level-Order-Traversal]]
|
2022-07-07 08:16:24 +08:00
|
|
|
|
- [[Leetcode Search-In-a-Binary-Tree]]
|
2022-07-05 09:31:16 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
##### Links:
|
2022-07-05 09:44:58 +08:00
|
|
|
|
- [Link to problem](https://leetcode.com/problems/maximum-depth-of-binary-tree/)
|
2022-07-05 09:31:16 +08:00
|
|
|
|
___
|
|
|
|
|
### Problem
|
2022-07-05 09:44:58 +08:00
|
|
|
|
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.
|
2022-07-05 09:31:16 +08:00
|
|
|
|
|
|
|
|
|
#### Examples
|
|
|
|
|
|
2022-07-05 09:44:58 +08:00
|
|
|
|
**Example 1:**
|
|
|
|
|
|
|
|
|
|
![](https://assets.leetcode.com/uploads/2020/11/26/tmp-tree.jpg)
|
|
|
|
|
|
|
|
|
|
**Input:** root = [3,9,20,null,null,15,7]
|
|
|
|
|
**Output:** 3
|
|
|
|
|
|
|
|
|
|
**Example 2:**
|
|
|
|
|
|
|
|
|
|
**Input:** root = [1,null,2]
|
|
|
|
|
**Output:** 2
|
|
|
|
|
|
2022-07-05 09:31:16 +08:00
|
|
|
|
#### Constraints
|
|
|
|
|
|
2022-07-05 09:44:58 +08:00
|
|
|
|
- The number of nodes in the tree is in the range `[0, 104]`.
|
|
|
|
|
- `-100 <= Node.val <= 100`
|
|
|
|
|
|
2022-07-05 09:31:16 +08:00
|
|
|
|
### Thoughts
|
|
|
|
|
|
|
|
|
|
> [!summary]
|
2022-07-05 09:44:58 +08:00
|
|
|
|
> This problem can be solved by #BFS or #DFS
|
|
|
|
|
|
|
|
|
|
BFS way:
|
|
|
|
|
Simply log the level in each while iteration.
|
|
|
|
|
|
|
|
|
|
DFS way: (Popular)
|
2022-07-05 09:55:27 +08:00
|
|
|
|
Use recursion:
|
|
|
|
|
- Base Case:
|
|
|
|
|
- root == nullptr: return 0;
|
|
|
|
|
- maxDepth(root) = max(maxDepth(root->left), maxDepth(root->right))
|
2022-07-05 09:31:16 +08:00
|
|
|
|
|
|
|
|
|
### Solution
|
2022-07-05 09:44:58 +08:00
|
|
|
|
|
|
|
|
|
DFS Recursion:
|
|
|
|
|
```cpp
|
2022-07-05 09:55:27 +08:00
|
|
|
|
/**
|
|
|
|
|
* 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) {
|
|
|
|
|
// DFS
|
|
|
|
|
if (!root) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return max(maxDepth(root->left), maxDepth(root->right)) + 1;
|
|
|
|
|
}
|
|
|
|
|
};
|
2022-07-05 09:44:58 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
BFS:
|
|
|
|
|
```cpp
|
|
|
|
|
/**
|
|
|
|
|
* 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) {
|
|
|
|
|
// BFS
|
|
|
|
|
int levels = 0;
|
|
|
|
|
queue<TreeNode *> pending;
|
|
|
|
|
TreeNode *ptr = root;
|
|
|
|
|
if (ptr)
|
|
|
|
|
pending.push(ptr);
|
|
|
|
|
|
|
|
|
|
while (!pending.empty()) {
|
|
|
|
|
levels++;
|
|
|
|
|
for (int i = 0, size = pending.size(); i < size; i++) {
|
|
|
|
|
ptr = pending.front();
|
|
|
|
|
pending.pop();
|
|
|
|
|
|
|
|
|
|
if (ptr->left)
|
|
|
|
|
pending.push(ptr->left);
|
|
|
|
|
if (ptr->right)
|
|
|
|
|
pending.push(ptr->right);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return levels;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
```
|