package leetcode;

/**
 * https://leetcode.com/problems/construct-string-from-binary-tree/
 */
public class Problem606 {
    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }

    public String tree2str(TreeNode t) {
        // TODO
        return "";
    }

    public static void main(String[] args) {
        Problem606 prob = new Problem606();

        TreeNode root = new TreeNode(1);
        root.left = new TreeNode(2);
        root.left.left = new TreeNode(4);
        root.right = new TreeNode(3);
        System.out.println(prob.tree2str(root)); // 1(2(4))(3)

        root = new TreeNode(1);
        root.left = new TreeNode(2);
        root.left.right = new TreeNode(4);
        root.right = new TreeNode(3);
        System.out.println(prob.tree2str(root)); // 1(2()(4))(3)
    }
}

@@ -331,16 +331,29 @@
     
-// TODO%0A
+if (t == null) %7B%0A    
     
@@ -375,109 +375,70 @@
     
-%7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem606 prob = new Problem606
+    %7D%0A        StringBuilder sb = new StringBuilder
 ();%0A
-%0A
         
 Tree
@@ -437,49 +437,35 @@
     
-TreeNode root = new TreeNode(1
+sb.append(t.val
 );%0A
+%0A
         
 root
@@ -464,76 +464,37 @@
     
-root.left = new TreeNode(2);%0A        root.left.left = new TreeNode(4
+String left = tree2str(t.left
 );%0A 
@@ -496,37 +496,39 @@
 t);%0A        
-root.
+String 
 right = 
 new TreeNode
@@ -519,33 +519,36 @@
 t = 
-new TreeNode(3
+tree2str(t.right
 );%0A
+%0A
         
 Syst
@@ -547,278 +547,275 @@
     
-System.out.println(prob.tree2str(root)); // 1(2(4))(3)%0A%0A        root = new TreeNode(1);%0A        root.left = new TreeNode(2);%0A        root.left.right = new TreeNode(4);%0A        root.right = new TreeNode(3);%0A        System.out.println(prob.tree2str(root)); // 1(2()(4))(3)
+if (left.equals(%22%22) && right.equals(%22%22)) %7B%0A            return sb.toString();%0A        %7D%0A        sb = sb.append(%22(%22).append(left).append(%22)%22);%0A        if (!right.equals(%22%22)) %7B%0A            sb.append(%22(%22).append(right).append(%22)%22);%0A        %7D%0A        return sb.toString();
 %0A   

Solve problem 606