Simple Java Divide & Conquer


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

Log in to reply
 

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