c++ 3ms solution with recursion


  • 0
    class Solution {
    public:
        vector<int> preorderTraversal(TreeNode* root) {
            vector<int> res;
            preorderTraversalCore(root, res);
            return res;
        }
        void preorderTraversalCore(TreeNode* root, vector<int>& res)
        {
            if(root==NULL)
                return;
            res.push_back(root->val);
            preorderTraversalCore(root->left, res);
            preorderTraversalCore(root->right, res);
        }
    };

Log in to reply
 

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