Skip to content

Commit 77e8d8d

Browse files
committed
Time: 8 ms (98.72%), Space: 23.5 MB (90.89%) - LeetHub
1 parent 966bf85 commit 77e8d8d

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10+
* };
11+
*/
12+
class Solution {
13+
string str;
14+
public:
15+
void dfs(TreeNode* t){
16+
if(t == NULL)
17+
return;
18+
19+
str += to_string(t->val);
20+
21+
if(t->left or t->right){
22+
str += "(";
23+
dfs(t->left);
24+
str += ")";
25+
}
26+
27+
if(t->right){
28+
str += "(";
29+
dfs(t->right);
30+
str += ")";
31+
}
32+
33+
}
34+
string tree2str(TreeNode* t) {
35+
dfs(t);
36+
return str;
37+
}
38+
};

0 commit comments

Comments
 (0)