-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBinary Tree Paths.cpp
More file actions
37 lines (35 loc) · 943 Bytes
/
Binary Tree Paths.cpp
File metadata and controls
37 lines (35 loc) · 943 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
27
28
29
30
31
32
33
34
35
36
37
/**
* 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 getPaths(TreeNode* root,string temp,vector<string> &vec)
{
if(root==NULL)
return ;
if(root->left==NULL && root->right==NULL)
{
//char ch=root->val+'0';
string ch=to_string(root->val);
temp=temp+ch;
vec.push_back(temp);
return;
}
//char ch=root->val+'0';
string ch=to_string(root->val);
temp=temp+ch+"->";
getPaths(root->left,temp,vec);
getPaths(root->right,temp,vec);
}
vector<string> binaryTreePaths(TreeNode* root) {
vector<string> vec;
getPaths( root,"",vec);
return vec;
}
};