The "Change the Root of a Binary Tree interview question" is a structural manipulation challenge. You are given a binary tree and a specific leaf node. You need to "reroot" the tree such that the provided leaf becomes the new root. This involves reversing the parent-child relationships along the path from the original root to the new root. Every node on this path must have its old parent as its new child.
Google asks the "Change the Root of a Binary Tree coding problem" to test a candidate's ability to manipulate complex pointer-based data structures. It evaluates whether you can perform non-trivial transformations on a tree without losing nodes or creating cycles. It requires a deep understanding of "Depth-First Search" and the ability to reason about bidirectional relationships in a tree.
This problem uses the Path Reversal and DFS patterns.
curr, make its old parent its new child.curr).Original: 1 -> 2, 2 -> 3 (1 is root, 3 is leaf).
Path: 1 -> 2 -> 3.
3 -> 2 -> 1.Rerooting is essentially a "Graph" operation. Sometimes it's easier to think of the tree as an undirected graph, find the path, and then reconstruct the tree from the new root using BFS or DFS.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Binary Tree Coloring Game | Medium | Solve | |
| Binary Tree Longest Consecutive Sequence II | Medium | Solve | |
| Delete Leaves With a Given Value | Medium | Solve | |
| Flip Equivalent Binary Trees | Medium | Solve | |
| Number of Good Leaf Nodes Pairs | Medium | Solve |