你有一棵二叉树,这棵二叉树有个小问题,其中有且只有一个无效节点,它的右子节点错误地指向了与其在同一层且在其右侧的一个其他节点。
给定一棵这样的问题二叉树的根节点 root
,将该无效节点及其所有子节点移除(除被错误指向的节点外),然后返回新二叉树的根结点。
自定义测试用例:
测试用例的输入由三行组成:
TreeNode root
int fromNode
(在correctBinaryTree
中不可见)int toNode
(在correctBinaryTree
中不可见)
当以 root
为根的二叉树被解析后,值为 fromNode
的节点 TreeNode
将其右子节点指向值为 toNode
的节点 TreeNode
。然后, root
传入 correctBinaryTree
的参数中。
示例 1:
输入: root = [1,2,3], fromNode = 2, toNode = 3 输出: [1,null,3] 解释: 值为 2 的节点是无效的,所以移除之。
示例 2:
输入: root = [8,3,1,7,null,9,4,2,null,null,null,5,6], fromNode = 7, toNode = 4 输出: [8,3,1,null,null,9,4,null,null,5,6] 解释: 值为 7 的节点是无效的,所以移除这个节点及其子节点 2。
提示:
- 树中节点个数的范围是
[3, 104]
。 -109 <= Node.val <= 109
- 所有的
Node.val
都是互不相同的。 fromNode != toNode
fromNode
和toNode
将出现在树中的同一层。toNode
在fromNode
的右侧。fromNode.right
在测试用例的树中建立后为null
。
记录父节点。
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def correctBinaryTree(self, root: TreeNode) -> TreeNode:
q = deque([root])
res = root
p = {}
while q:
n = len(q)
mp = {}
for _ in range(n):
node = q.popleft()
if node.val in mp:
left, father = p[mp[node.val]]
if left:
father.left = None
else:
father.right = None
return res
if node.left:
q.append(node.left)
p[node.left.val] = [True, node]
if node.right:
q.append(node.right)
p[node.right.val] = [False, node]
mp[node.right.val] = node.val
return res
优化,无需记录父节点。
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def correctBinaryTree(self, root: TreeNode) -> TreeNode:
q = deque([root])
while q:
n = len(q)
for _ in range(n):
node = q.popleft()
if node.right:
if node.right.right in q:
node.right = None
return root
q.append(node.right)
if node.left:
if node.left.right in q:
node.left = None
return root
q.append(node.left)
return root
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public TreeNode correctBinaryTree(TreeNode root) {
Deque<TreeNode> q = new ArrayDeque<>();
q.offer(root);
while (!q.isEmpty()) {
int n = q.size();
while (n-- > 0) {
TreeNode node = q.pollFirst();
if (node.right != null) {
if (node.right.right != null && q.contains(node.right.right)) {
node.right = null;
return root;
}
q.offer(node.right);
}
if (node.left != null) {
if (node.left.right != null && q.contains(node.left.right)) {
node.left = null;
return root;
}
q.offer(node.left);
}
}
}
return root;
}
}
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
TreeNode* correctBinaryTree(TreeNode* root) {
queue<TreeNode*> q;
q.push(root);
unordered_set<TreeNode*> s;
while (!q.empty())
{
int n = q.size();
while (n--)
{
TreeNode* node = q.front();
q.pop();
if (node->right)
{
if (s.count(node->right->right))
{
node->right = nullptr;
return root;
}
q.push(node->right);
s.insert(node->right);
}
if (node->left)
{
if (s.count(node->left->right))
{
node->left = nullptr;
return root;
}
q.push(node->left);
s.insert(node->left);
}
}
}
return root;
}
};