Sword Point offer series----serialized binary tree

Source: Internet
Author: User

Topic

* Implement two functions, respectively, to serialize and deserialize binary trees
* 1
/ \
2 3
/\ /\
4 5 6 7
*
* The result of serialization is 1,2,#,#,3,4,#,7,#,#,5,#,#,.
* The result of deserialization is the output of a binary tree.

1  PackageCom.exe7.offer;2 3 /**4 * Implement two functions, respectively, to serialize and deserialize binary trees5 * 16   / 7 2 38  /\    /9 4 5 6 7Ten  *                  One * The result of serialization is 1,2,#,#,3,4,#,7,#,#,5,#,#,.  A * The result of deserialization is the output of a binary tree.  -  * @authorWGS -  * the  */ -  Public classSerializebitree { -     Static classtreenode{ -         intVal=0; +TreeNode left=NULL; -TreeNode right=NULL; +TreeNode next=NULL; A          PublicTreeNode (intval) { at              This. val=Val; -         } -     } -      -     //serialization of pre-order -      PublicString Serialize (TreeNode proot) { in          -StringBuilder sb=NewStringBuilder (); to         if(proot==NULL){ +Sb.append ("#,"); -             returnsb.tostring (); the         } *Sb.append (proot.val+ ","); $ Sb.append (Serialize (Proot.left));Panax Notoginseng Sb.append (Serialize (Proot.right)); -         returnsb.tostring (); the          +     } A     //deserialization is equivalent to the process of creating a two-fork tree the     intP=-1; +      PublicTreeNode Deserialize (String strin) { -p++; $         if(p>strin.length ()) $             return NULL; -String[] Strarr=strin.split (","); -TreeNode node=NULL; the         if(!strarr[p].equals ("#")){ -Node=NewTreeNode (integer.valueof (strarr[p));Wuyinode.left=deserialize (strin); thenode.right=deserialize (strin); -         } Wu         returnnode; -          About     } $      Public voidPreorder (TreeNode node) { -         if(node==NULL)return; -System.out.print (node.val+ ""); - preorder (node.left); A preorder (node.right); +     } the      Public Static voidMain (string[] args) { -Serializebitree s=NewSerializebitree (); $          the         //deserialization theString strin= ("1,2,#,#,3,4,#,7,#,#,5,#,#,"); theTreeNode tempnode=s.deserialize (strin); theS.preorder (Tempnode);//Pre-sequence traversal -          inSystem.out.println ("------------------------"); the         //serialization results: 1,2,4,#,#,5,#,#,3,6,#,#,7,#,#, the /* About 1 the           /  the 2 3 the         /\    / + 4 5 6 7 -  * */ theTreeNode root=NewTreeNode (1);BayiTreeNode node1=NewTreeNode (2); theTreeNode node2=NewTreeNode (3); theTreeNode node3=NewTreeNode (4); -TreeNode node4=NewTreeNode (5); -TreeNode node5=NewTreeNode (6); theTreeNode node6=NewTreeNode (7); theRoot.left=node1;//2 theRoot.right=node2;//3 theNode1.left=node3;//4 -Node1.right=node4;//5 theNODE2.LEFT=NODE5;//6 theNode2.right=node6;//7 the         94String str=s.serialize (root); the System.out.println (str); the     } the 98}

Sword Point offer series----serialized binary tree

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.