Home › Company-wise questions › L&T Mindtree › Serialize and Deserialize Binary Tree ← L&T Mindtree bank
L&T Mindtree · Question 57 · Trees
Serialize and Deserialize Binary Tree Trees Java code ⏱ O(n) 💾 O(n)
Service-based Published and maintained by DSA-topics · Last updated 30 November 2026 · Report an error
About this bank These are classic technical-interview concepts compiled from publicly circulated company-wise question sets, rewritten with original explanations and compiler-ready solutions for educational practice; inclusion does not imply endorsement by the company. Spotted an error? Tell us .
Problem You are required to design algorithm to serialize and deserialize binary tree.
Approach Preorder with 'null' markers serializes; recursive consumption rebuilds.
Time O(n)Space O(n)
How it works 1 Emit values preorder with 'null' for missing children. 2 Rebuild by consuming tokens in the same order. Sample input / output Input:
root = [2, 1, 3]
Output:
2,1,null,null,3,null,null, Constraints 0 <= nodes <= 10^4.
Solution — read it, then walk it Flowchart Pseudocode
Time O(n)Space O(n)Loops 1Branches 5Returns 5source · Java
Scroll sideways for wide charts — the view follows the active step. ⤢ Expand
yes no ↻ repeat START import java.util.* DEFINE class Main DEFINE class Codec DEFINE String serialize(TreeNode root) StringBuilder sb = new StringBuilder() build(root, sb) RETURN sb.toString() DEFINE void build(TreeNode r, StringBuilder sb) if (r == null) sb.append("null,") RETURN sb.append(r.val).append(",") build(r.left, sb) build(r.right, sb) DEFINE TreeNode deserialize(String data) Queue<String> q = new ArrayDeque<>(Arrays.asList(data.split(","))) RETURN build(q) DEFINE TreeNode build(Queue<String> q) String s = q.poll() if (s.equals("null")) return null TreeNode node = new TreeNode(Integer.parseInt(s)) node.left = build(q) node.right = build(q) RETURN node DEFINE TreeNode buildTree(Integer[] values) if (values.length == 0 || values[0] == null) return null TreeNode root = new TreeNode(values[0]) java.util.Queue<TreeNode> q = new java.util.LinkedList<>() q.offer(root) DEFINE i = 1 while (!q.isEmpty() && i < values.length) TreeNode node = q.poll() if (i < values.length && values[i] != null) { node.left = ne… i++ if (i < values.length && values[i] != null) { node.right = ne… i++ RETURN root DEFINE class TreeNode DEFINE val TreeNode left, right TreeNode(int v) { val = v; } DEFINE static void main(String[] args) Codec codec = new Codec() TreeNode root = buildTree(new Integer[]{2, 1, 3}) System.out.println(codec.serialize(codec.deserialize(codec.serialize(root)))) start / end statement condition loop returns definition← Q56 · Binary Tree Right Side View Q58 · Number of Islands → Solution code Core logic highlighted in green
Language Python Javasource C++ C Core logic Full code Copy
Recommended resources Affiliate links · may earn us a commission
Classic DSA interview books The reference books interviewers themselves learned from — great next to the visualizers. See the books → Structured coding courses Video courses that pair well with visual practice — pick one language and go deep. Browse courses → Cloud hosting for side projects Ship the portfolio project that gets you hired — free credits for new accounts. Claim credits →