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
curhas a left child, then that child becomescur's right child. cur's original parent becomescur's left child. Note that in this process the original parent's pointer tocurbecomesnull, 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

给定二叉树和叶节点,重新构建树使其以叶节点为新根。从叶节点开始,若节点有左子树,将其变为右子树,原父节点变为左子节点。返回新根。需正确处理节点指针。举例说明了重构过程和解题思路。
最低0.47元/天 解锁文章
1906

被折叠的 条评论
为什么被折叠?



