1666. Change the Root of a Binary Tree
Last updated
Last updated
https://leetcode.com/problems/change-the-root-of-a-binary-tree
Given the root
of a binary tree and a leaf
node, reroot the tree so that the leaf
is the new root.
You can reroot the tree with the following steps for each node cur
on the path starting from the leaf
up to the root
excluding the root:
If cur
has a left child, then that child becomes cur
's right child.
cur
's original parent becomes cur
's left child. Note that in this process the original parent's pointer to cur
becomes null
, making it have at most one child.
Return the new root of the rerooted tree.
Note: Ensure that your solution sets the Node.parent
pointers correctly after rerooting or you will receive "Wrong Answer".
Example 1:
Example 2:
Constraints:
The number of nodes in the tree is in the range [2, 100]
.
-109 <= Node.val <= 109
All Node.val
are unique.
leaf
exist in the tree.