-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathBinary_tree_paths.cpp
More file actions
26 lines (26 loc) · 862 Bytes
/
Binary_tree_paths.cpp
File metadata and controls
26 lines (26 loc) · 862 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/**
* 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:
void binaryTreePathsUtil(TreeNode* root, string path, vector<string>& result) {
if(!root) return;
if(!root->left and !root->right) {
result.push_back(path + to_string(root->val));
return;
}
if(root->left) binaryTreePathsUtil(root->left, path + to_string(root->val) + "->", result);
if(root->right) binaryTreePathsUtil(root->right, path + to_string(root->val) + "->", result);
}
vector<string> binaryTreePaths(TreeNode* root) {
vector<string> result;
binaryTreePathsUtil(root, "", result);
return result;
}
};