class Solution {
fun tree2str(root: TreeNode?): String {
val sb = StringBuilder()
tree2str(root, sb)
return sb.toString()
}
fun tree2str(root: TreeNode?, sb: StringBuilder) {
if (root == null) return
sb.append(root.`val`)
if (root.left == null && root.right == null) return
sb.append("(")
tree2str(root.left, sb)
sb.append(")")
if (root.right != null) {
sb.append("(")
tree2str(root.right, sb)
sb.append(")")
}
}
}