If you work with graphics and eventually you want to pivot a binary tree using a different node as the root, this might be interesting to you. Here is the problem: Change the Root of a Binary Tree - LeetCode 1666. Change the Root of a Binary Tree Medium 6 14 Add to List Share 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. Note that it is guaranteed that cur will have at most one child. cur 's original parent becomes cur 's left 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,nul
Comments
Post a Comment