Created
September 2, 2017 16:17
-
-
Save cixuuz/a4df270691590fc1856d033045cca77f to your computer and use it in GitHub Desktop.
[606. Construct String from Binary Tree] #leetcode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public String tree2str(TreeNode t) { | |
if (t == null) return ""; | |
String left = tree2str(t.left); | |
String right = tree2str(t.right); | |
StringBuilder sb = new StringBuilder(); | |
sb.append(t.val); | |
if (right.equals("")) { | |
if (!left.equals("")) sb.append("(" + left + ")"); | |
} else { | |
sb.append("(" + left + ")"); | |
sb.append("(" + right + ")"); | |
} | |
return sb.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment