-
Notifications
You must be signed in to change notification settings - Fork 6
/
0094.cpp
48 lines (35 loc) · 968 Bytes
/
0094.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
43
44
45
46
47
48
// Solution 1 :-
class Solution {
public:
void in_order(TreeNode* root, vector<int>& values){
if(!root)
return;
in_order(root->left, values);
values.push_back(root->val);
in_order(root->right, values);
}
vector<int> inorderTraversal(TreeNode* root) {
vector<int> result;
in_order(root, result);
return result;
}
};
// Solution 2 :-
class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
vector<int> nodes;
stack<TreeNode*> st;
while (root || !st.empty()) {
while (root) {
st.push(root);
root = root -> left;
}
root = st.top();
st.pop();
nodes.push_back(root -> val);
root = root -> right;
}
return nodes;
}
};