notes/OJ notes/pages/Leetcode Maximum-Depth-Of-Binary-Tree.md

145 lines
2.6 KiB
Markdown
Raw Normal View History

2022-07-05 09:31:16 +08:00
# Leetcode Maximum-Depth-Of-Binary-Tree
#### 2022-07-05 09:25
> ##### Algorithms:
2022-09-03 15:41:36 +08:00
>
2022-07-05 09:31:16 +08:00
> #algorithm #BFS
2022-09-03 15:41:36 +08:00
>
2022-07-05 09:31:16 +08:00
> ##### Data structures:
2022-09-03 15:41:36 +08:00
>
> #DS #binary_tree
>
2022-07-05 09:31:16 +08:00
> ##### Difficulty:
2022-09-03 15:41:36 +08:00
>
2022-09-06 20:22:48 +08:00
> #coding_problem #difficulty_easy
2022-09-03 15:41:36 +08:00
>
2022-07-05 09:31:16 +08:00
> ##### Additional tags:
2022-09-03 15:41:36 +08:00
>
2022-07-05 09:31:16 +08:00
> #leetcode
2022-09-03 15:41:36 +08:00
>
2022-07-05 09:31:16 +08:00
> ##### Revisions:
2022-09-03 15:41:36 +08:00
>
2022-07-05 09:31:16 +08:00
> N/A
##### Related topics:
2022-09-03 15:41:36 +08:00
2022-07-05 09:31:16 +08:00
##### Links:
2022-09-03 15:41:36 +08:00
2022-07-05 09:44:58 +08:00
- [Link to problem](https://leetcode.com/problems/maximum-depth-of-binary-tree/)
2022-09-03 15:41:36 +08:00
---
2022-07-05 09:31:16 +08:00
### Problem
2022-09-03 15:41:36 +08:00
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-09-03 15:41:36 +08:00
- The number of nodes in the tree is in the range `[0, 104]`.
- `-100 <= Node.val <= 100`
2022-07-05 09:44:58 +08:00
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:
2022-09-03 15:41:36 +08:00
2022-07-05 09:55:27 +08:00
- Base Case:
2022-09-03 15:41:36 +08:00
- root == nullptr: return 0;
2022-07-05 09:55:27 +08:00
- 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:
2022-09-03 15:41:36 +08:00
2022-07-05 09:44:58 +08:00
```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:
2022-09-03 15:41:36 +08:00
2022-07-05 09:44:58 +08:00
```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;
}
};
2022-09-03 15:41:36 +08:00
```