forked from arkingc/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
94.cpp
39 lines (36 loc) · 908 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
/**
* Definition for a binary tree node.
* 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<int> res;
stack<TreeNode*> st;
if(root)
st.push(root);
TreeNode *curr = root;
while(curr || !st.empty()){
if(curr){
if(curr->left)
st.push(curr->left);
curr = curr->left;
}
else{
TreeNode *nd = st.top();
st.pop();
res.push_back(nd->val);
if(nd->right){
curr = nd -> right;
st.push(curr);
}
}
}
return res;
}
};