Simple Java recursion solution without helper


  • 0
    U
    public class Solution {
        public String tree2str(TreeNode t) {
            if (t == null)
                return "";
            if (t.left == null && t.right == null)
                return Integer.toString(t.val);
            if (t.right == null)
                return Integer.toString(t.val) + '(' + tree2str(t.left) + ')';
                
            return Integer.toString(t.val) + '(' + tree2str(t.left) + ')' + '(' + tree2str(t.right) + ')';
        }
    }
    

Log in to reply
 

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