notes/OJ notes/pages/Leetcode Binary-Tree-Inorder-Traversal.md

146 lines
2.6 KiB
Markdown
Raw Permalink Normal View History

2022-07-04 15:50:54 +08:00
# Leetcode Binary-Tree-Inorder-Traversal
#### 2022-07-04 15:42
> ##### Algorithms:
2022-09-03 15:41:36 +08:00
>
2022-07-04 15:50:54 +08:00
> #algorithm #DFS #DFS_inorder
2022-09-03 15:41:36 +08:00
>
2022-07-04 15:50:54 +08:00
> ##### Data structures:
2022-09-03 15:41:36 +08:00
>
> #DS #binary_tree
>
2022-07-04 15:50:54 +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-04 15:50:54 +08:00
> ##### Additional tags:
2022-09-03 15:41:36 +08:00
>
2022-07-04 15:50:54 +08:00
> #leetcode
2022-09-03 15:41:36 +08:00
>
2022-07-04 15:50:54 +08:00
> ##### Revisions:
2022-09-03 15:41:36 +08:00
>
2022-07-04 15:50:54 +08:00
> N/A
##### Related topics:
2022-09-03 15:41:36 +08:00
2022-07-04 15:50:54 +08:00
##### Links:
2022-09-03 15:41:36 +08:00
2022-07-04 15:50:54 +08:00
- [Link to problem](https://leetcode.com/problems/binary-tree-inorder-traversal/)
2022-09-03 15:41:36 +08:00
---
2022-07-04 15:50:54 +08:00
### Problem
2022-09-03 15:41:36 +08:00
2022-07-04 15:50:54 +08:00
Given the `root` of a binary tree, return _the inorder traversal of its nodes' values_.
#### Examples
2022-09-03 15:41:36 +08:00
2022-07-04 15:50:54 +08:00
**Example 1:**
![](https://assets.leetcode.com/uploads/2020/09/15/inorder_1.jpg)
**Input:** root = [1,null,2,3]
**Output:** [1,3,2]
**Example 2:**
**Input:** root = []
**Output:** []
**Example 3:**
**Input:** root = [1]
**Output:** [1]
#### Constraints
2022-09-03 15:41:36 +08:00
- The number of nodes in the tree is in the range `[0, 100]`.
- `-100 <= Node.val <= 100`
2022-07-04 15:50:54 +08:00
### Thoughts
> [!summary]
> This is a #DFS traversal problem
Many of them are same to [[Leetcode Binary-Tree-Preorder-Traversal]]
2022-09-03 15:41:36 +08:00
2022-07-04 15:50:54 +08:00
### Solution
Recursion
2022-09-03 15:41:36 +08:00
2022-07-04 15:50:54 +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 {
void inorder(TreeNode *root, vector<int> &answer) {
if (!root) {
return;
}
inorder(root->left, answer);
answer.push_back(root->val);
inorder(root->right, answer);
}
public:
vector<int> inorderTraversal(TreeNode *root) {
// Recursion.
vector<int> answer;
inorder(root, answer);
return answer;
}
};
```
Iteration
2022-09-03 15:41:36 +08:00
2022-07-04 15:50:54 +08:00
```cpp
2022-07-04 16:34:55 +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:
vector<int> inorderTraversal(TreeNode *root) {
// Iteration
stack<TreeNode *> pending;
vector<int> answer;
while (root || !pending.empty()) {
// traverse left first.
while (root) {
pending.push(root);
root = root->left;
}
root = pending.top();
pending.pop();
answer.push_back(root->val);
root = root->right;
}
return answer;
}
};
2022-07-04 15:50:54 +08:00
2022-09-03 15:41:36 +08:00
```