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

199 lines
4 KiB
Markdown
Raw Normal View History

2022-07-04 15:01:29 +08:00
# Leetcode Binary-Tree-Preorder-Traversal
#### 2022-07-04 14:51
> ##### Algorithms:
2022-09-03 15:41:36 +08:00
>
2022-07-04 15:50:54 +08:00
> #algorithm #DFS #DFS_preorder #Morris_traversal
2022-09-03 15:41:36 +08:00
>
2022-07-04 15:01:29 +08:00
> ##### Data structures:
2022-09-03 15:41:36 +08:00
>
2022-07-04 15:01:29 +08:00
> #DS #binary_tree
2022-09-03 15:41:36 +08:00
>
2022-07-04 15:01:29 +08:00
> ##### Difficulty:
2022-09-03 15:41:36 +08:00
>
2022-07-04 15:01:29 +08:00
> #coding_problem #difficulty-easy
2022-09-03 15:41:36 +08:00
>
2022-07-04 15:01:29 +08:00
> ##### Additional tags:
2022-09-03 15:41:36 +08:00
>
2022-07-04 15:50:54 +08:00
> #leetcode #CS_list_need_understanding
2022-09-03 15:41:36 +08:00
>
2022-07-04 15:01:29 +08:00
> ##### Revisions:
2022-09-03 15:41:36 +08:00
>
2022-07-04 15:01:29 +08:00
> N/A
##### Related topics:
2022-09-03 15:41:36 +08:00
2022-07-04 15:01:29 +08:00
##### Links:
2022-09-03 15:41:36 +08:00
2022-07-04 15:01:29 +08:00
- [Link to problem](https://leetcode.com/problems/binary-tree-preorder-traversal/)
2022-09-03 15:41:36 +08:00
---
2022-07-04 15:01:29 +08:00
### Problem
2022-09-03 15:41:36 +08:00
2022-07-04 15:01:29 +08:00
Given the `root` of a binary tree, return _the preorder traversal of its nodes' values_.
#### Examples
2022-09-03 15:41:36 +08:00
2022-07-04 15:01:29 +08:00
**Example 1:**
![](https://assets.leetcode.com/uploads/2020/09/15/inorder_1.jpg)
**Input:** root = [1,null,2,3]
**Output:** [1,2,3]
**Example 2:**
**Input:** root = []
**Output:** []
**Example 3:**
**Input:** root = [1]
**Output:** [1]
2022-09-03 15:41:36 +08:00
2022-07-04 15:01:29 +08:00
#### 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:01:29 +08:00
### Thoughts
> [!summary]
2022-09-03 15:41:36 +08:00
> This is a #binary_tree #DFS_preorder problem.
2022-07-04 15:01:29 +08:00
Preorder, means root is at the "Pre" position, so the order is:
- Search root node
- Search left sub-tree
- Search right sub-tree
2022-07-04 15:08:23 +08:00
2022-07-04 15:01:29 +08:00
The recursion version is easy to implement.
2022-07-04 15:32:45 +08:00
Use Stacks for the iteration method, because stacks are FILO, the **subtree will get traversed first.**
And remember to push **right subtree** first, so that the left one will be traversed first.
2022-07-04 15:50:54 +08:00
The Morris traversal needs more understandings.
2022-07-04 15:32:45 +08:00
2022-07-04 15:01:29 +08:00
### Solution
2022-09-03 15:41:36 +08:00
2022-07-04 15:32:45 +08:00
Iteration
2022-09-03 15:41:36 +08:00
2022-07-04 15:32:45 +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:
vector<int> preorderTraversal(TreeNode *root) {
if (!root) {
return {};
}
// Using stacks, since FILO, the latest gets served first.
vector<int> answer;
stack<TreeNode *> pending;
do {
if (root != nullptr) {
answer.push_back(root->val);
// nothing gets pushed if is nullptr;
pending.push(root->right);
pending.push(root->left);
}
root = pending.top();
pending.pop();
} while (root || !pending.empty());
return answer;
}
};
```
2022-09-03 15:41:36 +08:00
2022-07-04 15:08:23 +08:00
Recursion, using private funcs:
2022-09-03 15:41:36 +08:00
2022-07-04 15:08:23 +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:
vector<int> preorderTraversal(TreeNode *root) {
// Use two functions:
vector<int> answer;
preorder(root, answer);
return answer;
}
private:
void preorder(TreeNode *root, vector<int> &answer) {
if (root == nullptr) {
return;
}
answer.push_back(root->val);
preorder(root->left, answer);
preorder(root->right, answer);
}
};
```
2022-07-04 15:01:29 +08:00
Recursion:
2022-09-03 15:41:36 +08:00
2022-07-04 15:01:29 +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:
vector<int> preorderTraversal(TreeNode *root) {
// If the node is empty
if (root == nullptr) {
return {};
}
// First check root
vector<int> answer;
answer.push_back(root->val);
vector<int> left = preorderTraversal(root->left);
answer.insert(answer.end(), left.begin(), left.end());
vector<int> right = preorderTraversal(root->right);
answer.insert(answer.end(), right.begin(), right.end());
return answer;
}
};
2022-07-04 15:08:23 +08:00
```