My Simple Java Solution


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

Log in to reply
 

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