Correct a Binary Tree

You're given a binary tree that has been corrupted! 🔧 There's exactly one defective node where its right child incorrectly points to another node at the same depth, but further to the right (instead of pointing to its actual right child or null).

Your mission is to detect and remove this corrupted node along with its entire subtree, then return the corrected binary tree.

Key Points:
• The invalid node's right pointer goes to a node at the same level
• The target node is to the right of the invalid node
• Remove the invalid node and everything below it
• Keep the incorrectly pointed-to node (it's innocent!)

Example: If node 2's right child incorrectly points to node 5 (instead of node 3), remove node 2 and its subtree, but keep node 5 in its correct position.

Input & Output

example_1.py — Basic Invalid Node
$ Input: root = [1,2,3,4,null,null,null], fromNode = 2, toNode = 3
Output: [1,null,3]
💡 Note: Node 2's right child incorrectly points to node 3 (which should be at the same level). After removing node 2 and its subtree (including node 4), we get the corrected tree with root 1 having only a right child 3.
example_2.py — Deeper Tree
$ Input: root = [1,2,3,4,5,6,7], fromNode = 4, toNode = 5
Output: [1,null,3,null,null,6,7]
💡 Note: Node 4 incorrectly points to node 5. After removing node 4 (and any children it might have had), node 2 becomes a leaf, and the tree structure is preserved correctly.
example_3.py — Root is Invalid
$ Input: root = [1,2], fromNode = 1, toNode = 2
Output: null
💡 Note: The root node itself is invalid (pointing to its own child incorrectly). Since we remove the invalid node, the entire tree is removed, resulting in null.

Constraints

  • The number of nodes in the tree is in the range [2, 104]
  • 1 ≤ Node.val ≤ 104
  • There is exactly one invalid node
  • The invalid node's right child points to another node at the same depth
  • The pointed-to node is to the right of the invalid node

Visualization

Tap to expand
Correct a Binary Tree INPUT Corrupted Binary Tree 1 2 3 4 WRONG! root = [1,2,3,4,null,null,null] fromNode=2, toNode=3 Corrupted Node Valid Node ALGORITHM (BFS) 1 BFS Right-to-Left Traverse each level from right 2 Track Visited Nodes Use HashSet for seen nodes 3 Detect Invalid Node Right child already visited? 4 Remove Corrupted Node Set parent's pointer to null BFS Queue (Right to Left) 1 3 2 -- 3 seen! visited = {1, 3} when checking node 2 FINAL RESULT Corrected Tree 1 3 Removed Output: [1, null, 3] OK - Tree Corrected! Node 2 subtree removed Key Insight: By traversing RIGHT-TO-LEFT in BFS, we see the "target" node BEFORE the corrupted node. When a node's right child is already in our visited set, we found the corrupted node! Time: O(n) | Space: O(n) for the visited set and queue. TutorialsPoint - Correct a Binary Tree | BFS Approach
Asked in
Meta 25 Google 20 Amazon 15 Microsoft 10
23.5K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen