Two functions should be implemented to serialize and deserialize the binary tree. If the node is empty, use “#” instead. Public class SerializeTree {public static void main(String[] args) {// TODO auto-generated method stub TreeNode root = new TreeNode(1); root.left = null; root.right = new TreeNode(2); root.right.left = new TreeNode(3); root.right.right = null; String str = serialize(root); System.out.println(str); TreeNode node = deSerialize(str); System.out.println(node); } public static String serialize(TreeNode root){ StringBuilder sb = new StringBuilder(); if(root == null){ sb.append(“#,”); return sb.toString(); } sb.append(root.value+”,”); sb.append(serialize(root.left)); sb.append(serialize(root.right)); return sb.toString(); } public static int index = 0; public static TreeNode deSerialize(String str){ String[] strs = str.split(“,”); if(strs[index].equals(“#”)){ index++; return null; } TreeNode root = new TreeNode(Integer.parseInt(strs[index])); index++; root.left = deSerialize(str); root.right = deSerialize(str); return root; }} — — — — — — — — — — — — — — — — — — — — — the author: another source I actually exist: the original CSDN: blog.csdn.net/qq_24034545… Copyright notice: This article is the blogger’s original article, reprint please attach the blog link!

For more learning materials: Annalin1203