C++ 4ms iterative clean


  • 0
    L
    class Solution {
    public:
        vector<int> inorderTraversal(TreeNode *root) {
            vector<int> result;
            stack<TreeNode*> visit;
            set<TreeNode*> visited;
            if(root == NULL)
                return result;
            visit.push(root);
            while(!visit.empty()) {
                TreeNode* node = visit.top();
                if(visited.find(node) == visited.end()) {
                    visited.insert(node);
                    if(node->left != NULL)
                        visit.push(node->left);
                } else {
                    result.push_back(node->val);
                    visit.pop();
                    if(node->right != NULL)
                        visit.push(node->right);
                }
            }
            return result;
        }
    };

Log in to reply
 

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