Why I can past Run Code but when I submit solution I'll get TLE?


  • 0
    G
    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    public class Codec {
    
        // Encodes a tree to a single string.
        public String serialize(TreeNode root) {
            String res = "[";
            Queue<TreeNode> queue = new LinkedList<>();
            queue.add(root);
            while (!queue.isEmpty()) {
                TreeNode cur = queue.poll();
                if (cur == null) res += "null,";
                else {
                    res += cur.val + ",";
                    queue.add(cur.left);
                    queue.add(cur.right);
                }        
            }
            return res.substring(0, res.length() - 1) + "]";
        }
    
        // Decodes your encoded data to tree.
        public TreeNode deserialize(String data) {
            String[] arr = data.substring(1, data.length() - 1).split(",");
            TreeNode root = null;
            if (!arr[0].equals("null")) {
                root = new TreeNode(Integer.parseInt(arr[0]));
            }
            Queue<TreeNode> queue1 = new LinkedList<>(), queue2 = new LinkedList<>();
            queue1.add(root);
            int index = 1;
            while (index < arr.length) {
                while (index < arr.length && !queue1.isEmpty()) {
                    TreeNode node = queue1.poll();
                    TreeNode left = null, right = null;
                    if (!arr[index].equals("null")) left = new TreeNode(Integer.parseInt(arr[index]));
                    index++;
                    if (!arr[index].equals("null")) right = new TreeNode(Integer.parseInt(arr[index]));
                    index++;
                    node.left = left;
                    node.right = right;
                    if (left != null) queue2.add(left);
                    if (right != null) queue2.add(right);
                } 
                while (index < arr.length && !queue2.isEmpty()) {
                    TreeNode node = queue2.poll();
                    TreeNode left = null, right = null;
                    if (!arr[index].equals("null")) left = new TreeNode(Integer.parseInt(arr[index]));
                    index++;
                    if (!arr[index].equals("null")) right = new TreeNode(Integer.parseInt(arr[index]));
                    index++;
                    node.left = left;
                    node.right = right;
                    if (left != null) queue1.add(left);
                    if (right != null) queue1.add(right);
                }
            }
            return root;
        }
    }
    
    // Your Codec object will be instantiated and called as such:
    // Codec codec = new Codec();
    // codec.deserialize(codec.serialize(root));
    

Log in to reply
 

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