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
๐Ÿ” Binary Tree Detective: Find the ImpostorOne node is pointing to the wrong same-level node!1๐Ÿ‘‘ Chief2๐Ÿšซ Impostor3โœ… Legitimate45ILLEGAL CONNECTION!๐Ÿ•ต๏ธ Investigation Process1. ๐Ÿ‘€ Survey level 2: [5, 3]2. ๐Ÿ“ Mark 5 as seen โœ“3. ๐Ÿ“ Mark 3 as seen โœ“4. ๐Ÿ” Check level 1: [3, 2]5. ๐Ÿ“ Mark 3 as seen โœ“6. ๐Ÿšจ Node 2 points to seen node 5!7. โš–๏ธ Remove impostor 2 and descendantsโœ… Corrected Tree135๐ŸŽฏ Clean family tree!
Understanding the Visualization
1
Survey the Scene
Use level-order traversal to examine each generation of the family tree
2
Right-to-Left Investigation
In each generation, investigate from right to left, keeping track of who you've seen
3
Spot the Fraud
If someone points to a person you've already interviewed, that's the fraud!
4
Remove the Criminal
Remove the fraudulent person and their entire lineage from the family tree
5
Case Closed
Return the cleaned up, legitimate family tree
Key Takeaway
๐ŸŽฏ Key Insight: Since invalid nodes always point RIGHT, processing levels from right-to-left lets us catch 'future references' immediately - like a detective who interviews the rightmost family members first and catches anyone pointing to someone they've already met!
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