C++ iterative O(h)memory and O(n)time


#1
    vector<int> Solution::preorderTraversal(TreeNode* A) {
        vector<int>v;
        stack<TreeNode* >s;
        while(A!=NULL){s.push(A),v.push_back(A->val),A=A->left;}
        while(s.empty()^1){
            TreeNode* x=s.top();
            s.pop();
            x=x->right;
            while(x!=NULL){v.push_back(x->val),s.push(x),x=x->left;}
        }
        return v;
    }