Java solution using XML


  • -4
    Q

    Probably a massive overkill. I convert a tree into XML doc in serialize(), and convert it back to tree in deserialize()

     public class Codec {
            public String serialize(TreeNode root) {
        		if (root == null){
        			return null;
        		}
        		try{
        			org.w3c.dom.Document doc = javax.xml.parsers.DocumentBuilderFactory.newInstance().
        					newDocumentBuilder().newDocument();
        			doc.getDocumentElement();
        			org.w3c.dom.Element rootEl = doc.createElement("TreeNode");
        			rootEl.setAttribute("val", String.valueOf(root.val));
        			doc.appendChild(rootEl);
        			walkTree(root, rootEl, doc);
        			
        			javax.xml.transform.TransformerFactory tf = javax.xml.transform.TransformerFactory.newInstance();
        			javax.xml.transform.Transformer transformer = tf.newTransformer();
        			java.io.StringWriter writer = new java.io.StringWriter();
        			transformer.transform(new javax.xml.transform.dom.DOMSource(doc), 
        					new javax.xml.transform.stream.StreamResult(writer));
        			return writer.getBuffer().toString();
        		}catch(Exception e){
        			throw new RuntimeException(e);
        		}
        	}
        
        	public TreeNode deserialize(String data) {
        		if (data == null){
        			return null;
        		}
        		try{
        			javax.xml.parsers.DocumentBuilder builder = javax.xml.parsers.DocumentBuilderFactory.newInstance().newDocumentBuilder();;  
        	        org.w3c.dom.Document doc = builder.parse( new org.xml.sax.InputSource( new 
        	        		java.io.StringReader( data )) ); 
        	        org.w3c.dom.Element root = doc.getDocumentElement();
        	        TreeNode rootNode = new TreeNode(Integer.parseInt(root.getAttribute("val")));
        	        walkDoc(rootNode, root);
        	        return rootNode;
        		}catch(Exception e){
        			throw new RuntimeException(e);
        		}
        	}
        
        	private void walkTree(TreeNode n, org.w3c.dom.Element parent, org.w3c.dom.Document doc) {
        		if (n == null){
        			return;
        		}
        		if (n.left!=null){
        			org.w3c.dom.Element leftEl = doc.createElement("TreeNodeLeft");
        			leftEl.setAttribute("val",String.valueOf(n.left.val));
        			parent.appendChild(leftEl);
        			walkTree(n.left, leftEl, doc);
        		}
        		if (n.right!=null){
        			org.w3c.dom.Element rightEl = doc.createElement("TreeNodeRight");
        			rightEl.setAttribute("val",String.valueOf(n.right.val));
        			parent.appendChild(rightEl);
        			walkTree(n.right, rightEl, doc);
        		}
        	}
        	
        	private void walkDoc(TreeNode n, org.w3c.dom.Element parent) {
        		if (n == null){
        			return;
        		}
        		org.w3c.dom.NodeList list = parent.getChildNodes();
        		if (list!=null){
        			for (int i=0;i<list.getLength();i++){
        				org.w3c.dom.Node node = list.item(i);
        				if (node.getNodeName().equals("TreeNodeLeft")){
        					TreeNode leftNode = new TreeNode(Integer.parseInt(((org.w3c.dom.Element)node).getAttribute("val")));
        					n.left = leftNode;
        					walkDoc(leftNode, (org.w3c.dom.Element)node);
        				}
        				else if (node.getNodeName().equals("TreeNodeRight")){
        					TreeNode rightNode = new TreeNode(Integer.parseInt(((org.w3c.dom.Element)node).getAttribute("val")));
        					n.right = rightNode;
        					walkDoc(rightNode, (org.w3c.dom.Element)node);
        				}
        			}
        		}
        	}
        }

Log in to reply
 

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