Java simple recursive solution


  • 0
    N
        public String tree2str(TreeNode t) {
            String res = "";
            if(t != null) {
                res += String.valueOf(t.val);
                if(t.left != null) {
                    res += "(" + tree2str(t.left) + ")";
                }
                if(t.right != null) {
                    if(t.left == null) res += "()";
                    res += "(" + tree2str(t.right) + ")";
                }
            }
            return res;
        }
    

Log in to reply
 

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