simple solution to understand c++


  • 0
    class Solution {
    public:
        vector<int> preorderTraversal(TreeNode* root) {
            vector<TreeNode*> vt;
            vector<int> res;
            vt.push_back(root);
            int i=0;
            while(i<vt.size()){
                if(vt[i]!=NULL){res.push_back(vt[i]->val);
                    cout<<vt[i]->val<<endl;
                    vt.insert(vt.begin()+i+1,vt[i]->right);
                    vt.insert(vt.begin()+i+1,vt[i]->left);
                }
                i++;
            }
            return res;
        }
    };
    

Log in to reply
 

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