vault backup: 2022-07-04 16:34:55

This commit is contained in:
juan 2022-07-04 16:34:55 +08:00
parent d8342c2e2a
commit 0bb4b5d381

View file

@ -94,5 +94,40 @@ public:
Iteration
```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> 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;
}
};
```