C++ iterative method


  • -1
    L

    class Solution {
    public:
    vector<int> inorderTraversal(TreeNode* root) {
    vector<int> result;
    stack<TreeNode*> tempStack;
    while(!tempStack.empty()||root!=NULL){
    if (root!=NULL){
    tempStack.push(root);
    root = root->left;
    }
    else{
    root = tempStack.top();
    result.push_back((tempStack.top())->val);
    tempStack.pop();
    root = root->right;
    }
    }
    return result;
    }
    };


Log in to reply
 

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