vault backup: 2022-07-04 16:34:55
This commit is contained in:
parent
d8342c2e2a
commit
0bb4b5d381
|
@ -94,5 +94,40 @@ public:
|
||||||
|
|
||||||
Iteration
|
Iteration
|
||||||
```cpp
|
```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;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
```
|
```
|
Loading…
Reference in a new issue