Home › Company-wise questions › PayPal › Flatten Binary Tree to Linked List ← PayPal bank
PayPal · Question 63 · Trees
Flatten Binary Tree to Linked List Trees Java code ⏱ O(n) 💾 O(h)
Product-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 Flatten a binary tree to a linked list in-place following preorder traversal.
Approach Recursively flatten left and right subtrees and re-link nodes.
Time O(n)Space O(h)
How it works 1 Flatten both subtrees first. 2 Splice the flat left between root and old right. Sample input / output Input:
root = [1, 2, 5, 3, 4, null, 6]
Output:
[1, 2, 3, 4, 5, 6] Constraints 0 <= nodes <= 200; in place preorder.
Solution — read it, then walk it Flowchart Pseudocode
Time O(n)Space O(h)Loops 3Branches 5Returns 1source · Java
Scroll sideways for wide charts — the view follows the active step. ⤢ Expand
↻ repeat ↻ repeat ↻ repeat START import java.util.* DEFINE class Main DEFINE class TreeNode DEFINE val TreeNode left, right TreeNode(int v) { val = v; } 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 static void flatten(TreeNode root) if (root == null) return flatten(root.left) flatten(root.right) TreeNode tempRight = root.right root.right = root.left root.left = null TreeNode current = root while (current.right != null) current = current.right current.right = tempRight DEFINE static void main(String[] args) TreeNode root = buildTree(new Integer[]{1, 2, 5, 3, 4, null, 6}) flatten(root) StringBuilder sb = new StringBuilder("[") TreeNode c = root while (c != null) sb.append(c.val) c = c.right if (c != null) sb.append(", ") System.out.println(sb.append("]").toString()) END start / end statement condition loop returns definition← Q62 · Path Sum Q64 · Populating Next Right Pointers in Each Node → 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 →