Iterative solution using stack


  • 0
     vector<int> inorderTraversal(TreeNode* root){
            stack<TreeNode*> s;
            vector<int> res;
            while(!s.empty()|| root){
            //push left nodes on to the stack
                while(root){
                    s.push(root);
                    root=root->left;
                }
                root = s.top();
                s.pop();
                res.push_back(root->val);
                root=root->right;
            }
            return res;
        }
    

Log in to reply
 

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