s4553711
4/9/2017 - 3:23 PM

257.cpp

/**
 * 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> result;
    vector<string> path;
    vector<string> binaryTreePaths(TreeNode* root) {
        findPath(root);
        return result;
    }
    
    void findPath(TreeNode* root) {
        if (root == NULL) {
            return;
        }
        
        path.push_back(to_string(root->val));
        
        if (root->left == NULL && root->right == NULL) {
            stringstream ss;
            ss << path[0];
            for(int i = 1; i < path.size(); i++) {
                ss << "->" << path[i];
            }
            result.push_back(ss.str());
        }

        findPath(root->left);
        findPath(root->right);
        path.pop_back();
    }
};