返回介绍

solution / 1600-1699 / 1666.Change the Root of a Binary Tree / README_EN

发布于 2024-06-17 01:03:16 字数 5663 浏览 0 评论 0 收藏 0

1666. Change the Root of a Binary Tree

中文文档

Description

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.

Solutions

Solution 1

"""
# Definition for a Node.
class Node:
  def __init__(self, val):
    self.val = val
    self.left = None
    self.right = None
    self.parent = None
"""


class Solution:
  def flipBinaryTree(self, root: "Node", leaf: "Node") -> "Node":
    cur = leaf
    p = cur.parent
    while cur != root:
      gp = p.parent
      if cur.left:
        cur.right = cur.left
      cur.left = p
      p.parent = cur
      if p.left == cur:
        p.left = None
      elif p.right == cur:
        p.right = None
      cur = p
      p = gp
    leaf.parent = None
    return leaf
/*
// Definition for a Node.
class Node {
  public int val;
  public Node left;
  public Node right;
  public Node parent;
};
*/

class Solution {
  public Node flipBinaryTree(Node root, Node leaf) {
    Node cur = leaf;
    Node p = cur.parent;
    while (cur != root) {
      Node gp = p.parent;
      if (cur.left != null) {
        cur.right = cur.left;
      }
      cur.left = p;
      p.parent = cur;
      if (p.left == cur) {
        p.left = null;
      } else if (p.right == cur) {
        p.right = null;
      }
      cur = p;
      p = gp;
    }
    leaf.parent = null;
    return leaf;
  }
}
/*
// Definition for a Node->
class Node {
public:
  int val;
  Node* left;
  Node* right;
  Node* parent;
};
*/

class Solution {
public:
  Node* flipBinaryTree(Node* root, Node* leaf) {
    Node* cur = leaf;
    Node* p = cur->parent;
    while (cur != root) {
      Node* gp = p->parent;
      if (cur->left) {
        cur->right = cur->left;
      }
      cur->left = p;
      p->parent = cur;
      if (p->left == cur) {
        p->left = nullptr;
      } else if (p->right == cur) {
        p->right = nullptr;
      }
      cur = p;
      p = gp;
    }
    leaf->parent = nullptr;
    return leaf;
  }
};
/**
 * // Definition for a Node.
 * function Node(val) {
 *  this.val = val;
 *  this.left = null;
 *  this.right = null;
 *  this.parent = null;
 * };
 */

/**
 * @param {Node} node
 * @return {Node}
 */
var flipBinaryTree = function (root, leaf) {
  let cur = leaf;
  let p = cur.parent;
  while (cur != root) {
    const gp = p.parent;
    if (cur.left != null) {
      cur.right = cur.left;
    }
    cur.left = p;
    p.parent = cur;
    if (p.left == cur) {
      p.left = null;
    } else if (p.right == cur) {
      p.right = null;
    }
    cur = p;
    p = gp;
  }
  leaf.parent = null;
  return leaf;
};
/*
// Definition for a Node.
public class Node {
  public int val;
  public Node left;
  public Node right;
  public Node parent;
}
*/

public class Solution {
  public Node FlipBinaryTree(Node root, Node leaf) {
    Node cur = leaf;
    Node p = cur.parent;
    while (cur != root) {
      Node gp = p.parent;
      if (cur.left != null) {
        cur.right = cur.left;
      }
      cur.left = p;
      p.parent = cur;
      if (p.left == cur) {
        p.left = null;
      } else if (p.right == cur) {
        p.right = null;
      }
      cur = p;
      p = gp;
    }
    leaf.parent = null;
    return leaf;
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文