Binary Tree Paths

https://leetcode.com/problems/binary-tree-paths/

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:

    vector<string> binaryTreePaths(TreeNode* root) {
        if(root==NULL)
            return res;
        string temp=to_string(root->val);
        if(root->left==NULL&&root->right==NULL)
            res.push_back(temp);
        if(root->left!=NULL)
            work(root->left,temp);
        if(root->right!=NULL)
            work(root->right,temp);
        
        return res;    
    }
    vector<string> res;
    void work(TreeNode * root,string temp)
    {
        temp+="->";
        temp+=to_string(root->val);
        if(root->left==NULL&&root->right==NULL)
        {
            res.push_back(temp);
        }
        if(root->left!=NULL)
        {
            work(root->left,temp);
        }
        if(root->right!=NULL)
        {
            work(root->right,temp);
        }
    }
    
};

  

原文地址:https://www.cnblogs.com/aguai1992/p/5029487.html