0094.cpp (520B)
1 class Solution { 2 public: 3 vector<int> inorderTraversal(TreeNode *root) { 4 vector<int> res; 5 stack<TreeNode *> st; 6 7 while (true) { 8 while (root) { 9 st.push(root); 10 root = root->left; 11 } 12 13 if (!st.empty()) { 14 root = st.top(); 15 st.pop(); 16 res.push_back(root->val); 17 root = root->right; 18 } else 19 return res; 20 } 21 22 return res; 23 } 24 };