Just do normal DFS:
cur
cur->right
cur->left
Because we use stack, cur
will visit the last pushed node.
For preorder traversal, it is this
-> left
-> right
.
Thus, we should push the left child to the stack later.
vector<int> preorderTraversal(TreeNode* root) {
vector<int> ans;
TreeNode* cur = root;
stack<TreeNode*> s;
while (cur || !s.empty()) {
if (cur) {
ans.push_back(cur->val);
s.push(cur->right);
s.push(cur->left);
}
cur = s.top(); s.pop();
}
return ans;
}
Do mirrored inorder traversal and reverse the results:
std::reserve
: http://www.cplusplus.com/reference/algorithm/reverse/
Note that insert
for vector is O(N) instead of O(1) for push_back
vector<int> postorderTraversal(TreeNode* root) {
vector<int> ans;
stack<TreeNode*> s;
TreeNode* cur = root;
while (cur || !s.empty()) {
if (cur) {
ans.push_back(cur->val);
s.push(cur->left);
s.push(cur->right);
}
cur = s.top(); s.pop();
}
reverse(ans.begin(),ans.end());
return ans;
}
vector<int> postorderTraversal(TreeNode* root) {
vector<int> ans;
stack<TreeNode*> s; // no null in stack
TreeNode* cur = root;
while (cur || !s.empty()) {
if (cur) {
if (cur->right) s.push(cur->right);
s.push(cur);
cur = cur-> left;
} else {
cur = s.top(); s.pop();
// make sure cur->right is processed before cur
if (cur->right && !s.empty() && cur->right == s.top()) {
s.pop();
s.push(cur);
cur = cur->right;
} else {
ans.push_back(cur->val);
cur = NULL; // always process node from stack
}
}
}
return ans;
}
vector<int> inorderTraversal(TreeNode* root) {
vector<int> ans;
stack<TreeNode*> s;
TreeNode* cur = root;
while (!s.empty() || cur) {
// check and add left child
if (cur) {
s.push(cur);
cur = cur-> left;
} else {
// reach the left end OR cur (parent->right) is null
// output from stack and go to right child
cur = s.top(); s.pop();
ans.push_back(cur->val);
cur = cur->right;
}
}
return ans;
}
simple BFS:
vector<int> preorderTraversal(TreeNode* root) {
vector<int> ans;
TreeNode* cur = root;
queue<TreeNode*> q;
while (cur || !q.empty()) {
if (cur) {
ans.push_back(cur->val);
q.push(cur->left);
q.push(cur->right);
}
cur = q.front(); q.pop();
}
return ans;
}
or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
 | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | ![]() |
Emoji list | |
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?
Please give us some advice and help us improve HackMD.
Do you want to remove this version name and description?
Syncing