-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBinaryTreePaths_257.cpp
51 lines (45 loc) · 1.4 KB
/
BinaryTreePaths_257.cpp
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
~ Author : leetcode.com/tridib_2003/
~ Problem : 257. Binary Tree Paths
~ Link : https://leetcode.com/problems/binary-tree-paths/
*/
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
vector<string> res;
void RootToLeaf(TreeNode *root, vector<int> path, int pathLen) {
if (root == NULL)
return;
path.push_back(root -> val);
pathLen++;
if (root -> left == NULL && root -> right == NULL) {
string pathStr = "";
for (int i = 0; i < pathLen; ++i) {
if (i != (pathLen - 1))
pathStr += to_string(path[i]) + "->";
else
pathStr += to_string(path[i]);
}
res.emplace_back(pathStr);
}
else {
RootToLeaf(root -> left, path, pathLen);
RootToLeaf(root -> right, path, pathLen);
}
}
vector<string> binaryTreePaths(TreeNode* root) {
vector<int> path;
RootToLeaf(root, path, 0);
return res;
}
};