Skip to content

Latest commit

 

History

History

1666

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

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:

  1. If cur has a left child, then that child becomes cur's right child.
  2. 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:

Input: root = [3,5,1,6,2,0,8,null,null,7,4], leaf = 7
Output: [7,2,null,5,4,3,6,null,null,null,1,null,null,0,8]

Example 2:

Input: root = [3,5,1,6,2,0,8,null,null,7,4], leaf = 0
Output: [0,1,null,3,8,5,null,null,null,6,2,null,null,7,4]

 

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.

Companies: Google

Related Topics:
Tree, Depth-First Search, Binary Tree

Solution 1.

// OJ: https://leetcode.com/problems/change-the-root-of-a-binary-tree
// Author: github.com/lzl124631x
// Time: O(H)
// Space: O(1)
class Solution {
public:
    Node* flipBinaryTree(Node* root, Node * leaf) {
        Node *cur = leaf, *p = leaf->parent;
        cur->parent = nullptr;
        if (p->left == cur) p->left = nullptr;
        else p->right = nullptr;
        while (cur != root) {
            if (cur->left) {
                cur->right = cur->left;
                cur->left = nullptr;
            }
            cur->left = p;
            auto pp = p->parent;
            p->parent = cur;
            if (pp) {
                if (pp->left == p) pp->left = nullptr;
                else pp->right = nullptr;
            }
            cur = p;
            p = pp;
        }
        return leaf;
    }
};