Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit027cb6c

Browse files
Update 0606-construct-string-from-binary-tree.java
1 parentb1c2bdf commit027cb6c

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

‎java/0606-construct-string-from-binary-tree.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,25 @@ public String tree2str(TreeNode root) {
99

1010
returnoutput;
1111
}
12+
}
13+
/* Alternative Solution
14+
-----------------------------------------------------------------*/
15+
classSolution {
16+
publicStringtree2str(TreeNoderoot) {
17+
StringBuilderres =newStringBuilder();
18+
dfs(root,res);
19+
returnres.toString().substring(1,res.length()-1);
20+
}
21+
privatevoiddfs(TreeNoderoot,StringBuilderres){
22+
if(root ==null)
23+
return;
1224

13-
25+
res.append("(");
26+
res.append(root.val);
27+
if(root.left ==null &&root.right !=null)
28+
res.append("()");
29+
dfs(root.left,res);
30+
dfs(root.right,res);
31+
res.append(")");
32+
}
1433
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp