c++ solution


  • 0
    B
    class Solution {
    public:
        vector<string> result;
        
        void dfs(TreeNode *root, string cur){
            
            if(!root->left && !root->right) { 
                result.push_back(cur); return;
            }
            
            if(root->left){
                dfs(root->left, cur+"->"+to_string(root->left->val));
            }
            
            if(root->right){
                dfs(root->right, cur+"->"+to_string(root->right->val));
            }
            
            return ;
        }
        
        vector<string> binaryTreePaths(TreeNode* root) {
            if(!root) return result;
            
            dfs(root,to_string(root->val));
            
            return result;
        }
    };
    
    
    

Log in to reply
 

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