Share my simple c++ solution


  • 3
    G
    class Solution {
    public:
        vector<string> binaryTreePaths(TreeNode* root) {
            vector<string> ret;
            if(root) dfs(ret, "", root);
            return ret;
        }
        
        void dfs(vector<string> &ret, string path, TreeNode* root){
            path += to_string(root->val);
            if(!root->left && !root->right){
                ret.push_back(path);
            }else{
                if(root->left) dfs(ret, path + "->", root->left);
                if(root->right) dfs(ret, path + "->", root->right);
            }
        }
    };

Log in to reply
 

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