1660. Correct a Binary Tree
On This Page
# 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:
def dfs(root):
if root is None or root.right in vis:
return None
vis.add(root)
root.right = dfs(root.right)
root.left = dfs(root.left)
return root
vis = set()
return dfs(root)