Home › Company-wise questions › PayPal › Populating Next Right Pointers in Each Node ← PayPal bank
PayPal · Question 64 · Trees
Populating Next Right Pointers in Each Node Trees Java code ⏱ O(n) 💾 O(1)
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 Populate each next pointer to point to its next right node in a perfect binary tree.
Approach Use level order traversal or constant space approach. (Constant Space): walk each level via next links.
Time O(n)Space O(1)
How it works 1 Each level is already linked when its children wire up. 2 Left child points to right sibling; right reaches across via parent next. Sample input / output Input:
root = [1, 2, 3, 4, 5, 6, 7]
Output:
[4, 5, 6, 7] Constraints Perfect binary tree; 0 <= nodes <= 4095; O(1) extra space.
Solution — read it, then walk it Flowchart Pseudocode
Time O(n)Space O(1)Loops 3Branches 2source · Java
Scroll sideways for wide charts — the view follows the active step. ⤢ Expand
yes no ↻ repeat ↻ repeat ↻ repeat START import java.util.* DEFINE class Main DEFINE class TreeLinkNode DEFINE val TreeLinkNode left, right, next TreeLinkNode(int v) { val = v; } DEFINE static void connect(TreeLinkNode root) if (root == null) return TreeLinkNode leftmost = root while (leftmost.left != null) TreeLinkNode head = leftmost while (head != null) head.left.next = head.right if (head.next != null) head.right.next = head.next.left head = head.next leftmost = leftmost.left DEFINE static void main(String[] args) TreeLinkNode n1 = new TreeLinkNode(1) TreeLinkNode n2 = new TreeLinkNode(2) TreeLinkNode n3 = new TreeLinkNode(3) TreeLinkNode n4 = new TreeLinkNode(4) TreeLinkNode n5 = new TreeLinkNode(5) TreeLinkNode n6 = new TreeLinkNode(6) TreeLinkNode n7 = new TreeLinkNode(7) n1.left = n2; n1.right = n3 n2.left = n4; n2.right = n5 n3.left = n6; n3.right = n7 connect(n1) StringBuilder sb = new StringBuilder("[") TreeLinkNode c = n4 while (c != null) sb.append(c.val) c = c.next sb.append(c != null ? ", " : "") System.out.println(sb.append("]").toString()) END start / end statement condition loop returns definition← Q63 · Flatten Binary Tree to Linked List Q65 · Validate Binary Search Tree → 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 →