Correct a Binary Tree - Problem
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!)
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
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!
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code