class Solution { public: /** * @param root: the root of the binary tree * @return: all root-to-leaf paths */ vector<string> binaryTreePaths(TreeNode * root) { // write your code here vector<string> res; if (!root) return res; if (root->left == NULL && root->right == NULL) res.push_back(to_string(root->val)); string head = to_string(root->val); vector<string> left = binaryTreePaths(root->left), right = binaryTreePaths(root->right); for (int i = 0; i < left.size(); i++) left[i] = head + "->" + left[i];
for (int i = 0; i < right.size(); i++) right[i] = head + "->" + right[i]; res.insert(res.end(), left.begin(), left.end()); res.insert(res.end(), right.begin(), right.end()); return res;
} };
-------------end of file
thanks for reading-------------