Recursion, C++, 0-4ms


  • 0
    H
    class Solution { 
    public: 
        vector<int> ans; 
        void repreTra(TreeNode* root) { 
            if (root != NULL) { 
                ans.push_back(root->val); 
                repreTra(root->left); 
                repreTra(root->right); 
            }
        } 
        vector preorderTraversal(TreeNode* root) { 
            repreTra(root); 
            return ans; 
        } 
    };

Log in to reply
 

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