Iterative C++ solution


  • 3
    G
    class Solution {
    public:
      vector<int> inorderTraversal(TreeNode* root) {
        vector<int> ret;
        stack<TreeNode *> s;
        for (TreeNode *n = root; n || !s.empty(); n = n->right) {
          for (; n; n = n->left) s.push(n);
          n = s.top(); s.pop();
          ret.push_back(n->val);
        }
        return ret;
      }
    };

Log in to reply
 

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