Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Leetcode 297. Serialize and Deserialize Binary Tree
//'main' method must be in a class 'Rextester'. //Compiler version 1.8.0_111 //Leetcode 297. Serialize and Deserialize Binary Tree import java.util.*; import java.lang.*; class Node{ int val; Node left; Node right; Node(int v){ val=v; } } class Rextester { public static void main(String args[]) { System.out.println("Hello, World!"); /*Node n = new Node(1); n.left = new Node(2); n.right = new Node(3); n.left.left=null; n.left.right=null; n.right.left = new Node(4); n.right.right = new Node(5); n.right.left.left = null; n.right.left.right = null; n.right.right.left = null; n.right.right.right = null; System.out.println(serialize(n));*/ Node root = deSerialize("1,2,null,null,3,4,null,null,5,null,null,"); } public static String serialize(Node root) { return recSerialize(root,""); } public static String recSerialize(Node n,String str){ if(n==null){ str+="null,"; return str; } str+=n.val+","; str=recSerialize(n.left,str); str=recSerialize(n.right,str); return str; } public static Node deSerialize(String str){ String[] tokens = str.split(","); List<String> lst = new ArrayList<String>(Arrays.asList(tokens)); return recDeSerialize(lst); } public static Node recDeSerialize(List<String> lst){ if(lst.get(0).equals("null")){ lst.remove(0); return null; } Node newNode = new Node(Integer.valueOf(lst.get(0))); lst.remove(0); newNode.left = recDeSerialize(lst); newNode.right = recDeSerialize(lst); return newNode; } }
run
|
edit
|
history
|
help
0
ekse
Invert sign and add
[Java] Coding - Reverse CODE - Mathematical Method
Basic LinkedList in Java (rewritten using for-loops)
With unicode change
Coding Numbers - Duplicate - Array
LunarLanding.java
2A
Lexisort
Car Sensors