Short c++ answer. Can I do it like this? (accepted already)


  • 0
    Y
    class Solution {
    public:
        vector<int> result;
        vector<int> preorderTraversal(TreeNode* root) {
            if(root){
                
                result.push_back(root->val);
                preorderTraversal(root->left);
                preorderTraversal(root->right);
            }
            
            return result;
        }
    };

Log in to reply
 

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