Java runtime beats 99% of submissions


  • 5
    S

    import java.util.StringTokenizer;
    public class Codec {

    // Encodes a tree to a single string.
    public String serialize(TreeNode root) {
        StringBuilder sb = new StringBuilder();
        serialize(root, sb);
        return sb.toString();
    }
    
    private void serialize(TreeNode root, StringBuilder sb) {
        if (root == null)
            sb.append("# ");
        else {
            sb.append(root.val + " ");
            serialize(root.left, sb);
            serialize(root.right, sb);
        }
    }
    
    // ADD "import java.util.StringTokenizer;" AT TOP ==========================
    // Decodes your encoded data to tree.
    public TreeNode deserialize(String data) {
        if (data == null || data.length() == 0) return null;
        StringTokenizer st = new StringTokenizer(data, " ");
        return deserialize(st);
    }
    
    private TreeNode deserialize(StringTokenizer st) {
        if (!st.hasMoreTokens())
            return null;
        String s = st.nextToken();
        if (s.equals("#"))
            return null;
        TreeNode root = new TreeNode(Integer.valueOf(s));
        root.left = deserialize(st);
        root.right = deserialize(st);
        
        return root;
    }
    

    }


  • 0

    Nice solution using pre-order serialization and de-serialization


Log in to reply
 

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