1 // 16ms 2 /** 3 * Definition for binary tree 4 * struct TreeNode { 5 * int val; 6 * TreeNode *left; 7 * TreeNode *right; 8 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 9 * }; 10 */ 11 class Solution { 12 public: 13 vector<int> inorderTraversal(TreeNode *root) { 14 // Start typing your C/C++ solution below 15 // DO NOT write int main() function 16 stack<TreeNode *> s; 17 18 vector <int> v; 19 TreeNode *top; 20 if(root==NULL) 21 return v; 22 23 s.push(root); 24 int f=0; 25 while(!s.empty()) 26 { 27 top=s.top(); 28 if(top->left&&f==0) 29 { 30 s.push(top->left); 31 } 32 else 33 { 34 v.push_back(top->val); 35 s.pop(); 36 f=1; 37 if(top->right) 38 { 39 s.push(top->right); 40 f=0; 41 } 42 } 43 } 44 return v; 45 } 46 };