-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path94.cpp
42 lines (38 loc) · 904 Bytes
/
94.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include<bits/stdc++.h>
using namespace std;
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
vector<TreeNode*> stack;
vector<int> res;
TreeNode* q = root;
while (q || stack.size() != 0) {
while (q) {
stack.push_back(q);
q = q->left;
}
if (stack.size()) {
q = stack.back();
res.push_back(q->val);
q = q->right;
stack.pop_back();
}
}
return res;
}
};