Skip to content

Instantly share code, notes, and snippets.

@cixuuz
Created September 2, 2017 16:17
Show Gist options
  • Save cixuuz/a4df270691590fc1856d033045cca77f to your computer and use it in GitHub Desktop.
Save cixuuz/a4df270691590fc1856d033045cca77f to your computer and use it in GitHub Desktop.
[606. Construct String from Binary Tree] #leetcode
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