Clean 4ms C++ solution


  • 1
    Y
    class Solution {
    public:
    vector<string> binaryTreePaths(TreeNode* root) {
        vector<string> result;
        string path = "";
        pathHelper(root,result,path);
        return result;
    }
    
    void pathHelper(TreeNode* root, vector<string> &result, string path){
        if(root == NULL){
            return;
        }
        path += to_string(root->val);
        if(root->left == NULL && root->right == NULL){
            result.push_back(path);
            return;
        }
        else{
            path += "->";
            pathHelper(root->left,result,path);
            pathHelper(root->right,result,path);
        }
    }
    };

Log in to reply
 

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