C++ solution using recursion


  • 0
    S
    class Solution {
        vector<int> soln;
    public:
        void preorderRecurse(TreeNode* root){
            if (!root) return;
            soln.push_back(root->val);
            preorderRecurse(root->left);
            preorderRecurse(root->right);
        }
    
        vector<int> preorderTraversal(TreeNode* root) {
           preorderRecurse(root);
           return soln;
        }
    };

Log in to reply
 

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