Share my C++ solution,very easy to understand


  • 4
    V
    class Solution {
    public:
        vector<int> inorderTraversal(TreeNode* root) {
            vector<int> ret;
            stack<TreeNode *> s;
            TreeNode *temp = root;
            
            while (!s.empty() || temp != NULL)
            {
                while (temp != NULL)
                {
                    s.push(temp);
                    temp = temp->left;
                }
                
                temp = s.top();
                ret.push_back(temp->val);
                s.pop();
                temp = temp->right;
            }
            
            return ret;
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.