AC Java 17ms. Beats 100%


  • 0
    K
        public String tree2str(TreeNode t) {
            StringBuilder sb = new StringBuilder();
            tree2str(t, sb);
            return sb.toString();
        }
        
        private void tree2str(TreeNode t, StringBuilder sb) {
            if(t == null) return;
            if(t.left == null && t.right == null) { sb.append(t.val); return; }
            sb.append(t.val);
            if(t.left == null) sb.append("()");
            else { sb.append("("); tree2str(t.left, sb); sb.append(")"); }
            if(t.right != null) { sb.append("("); tree2str(t.right, sb); sb.append(")"); }
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.