Java Level Order Traversal | Readable Code
\npublic class Codec {\n    /**\n     * Traverses binary tree in level order \n     * Stores node values in string(comma separated)\n     */\n    private String
```\npublic class Codec {\n    /**\n     * Traverses binary tree in level order \n     * Stores node values in string(comma separated)\n     */\n    private String levelOrderTrav(TreeNode root){\n        StringBuilder sb = new StringBuilder();\n        Queue<TreeNode> que = new LinkedList<>();\n        que.add(root);\n        TreeNode itr = null;\n        while(que.size()>0){\n            itr = que.remove();\n            if(itr!=null) {\n                sb.append(itr.val+",");\n                que.add(itr.left);\n                que.add(itr.right);\n            }\n            else sb.append("n,");\n        }\n        return sb.substring(0, sb.length()-1).toString();\n    }\n    // Encodes a tree to a single string.\n    public String serialize(TreeNode root) {\n        return levelOrderTrav(root);\n    }\n    /**\n     * Contructs Binary Tree from leverl order traversalcomma separated string)\n     */\n    private TreeNode constructBstFromLevelOrderTrav(String data){\n        String[] nodeValues = data.split(",");\n        if(nodeValues[0].equals("n")) return null; //zero nodes\n        Queue<TreeNode> que = new LinkedList<>();\n        TreeNode root = new TreeNode(Integer.parseInt(nodeValues[0]));\n        que.add(root);\n        TreeNode itr = null;\n        int indx = 1;\n        while(que.size()>0){\n            itr = que.remove();\n            String valL = nodeValues[indx++];\n            String valR = nodeValues[indx++];\n            if(!valL.equals("n")){\n                itr.left = new TreeNode(Integer.parseInt(valL));\n                que.add(itr.left);\n            }\n            if(!valR.equals("n")){\n                itr.right = new TreeNode(Integer.parseInt(valR));\n                que.add(itr.right);\n            }\n        }\n        return root;\n    }\n    // Decodes your encoded data to tree.\n    public TreeNode deserialize(String data) {\n        return constructBstFromLevelOrderTrav(data);\n    }\n}\n```
['Java']