6 Lines Easy Understand Java method


  • 0
    C
    public String tree2str(TreeNode t) {
            if (t == null) return "";
            if (t.left == null && t.right == null) return Integer.toString(t.val);
            String cur = t.val + "(" + tree2str(t.left) + ")";
            if (t.right != null)
                cur += "(" + tree2str(t.right) + ")";
            return cur;
    }
    

Log in to reply
 

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