Sobes.tech
Junior — Senior

Finding the corresponding node in a clone of a binary tree

livecode

Task condition

Given two binary trees: the original and its clone, as well as a reference to a target node in the original tree. The task is to find the node in the cloned tree that is an exact copy of the target, and return a reference to it.

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
    def getTargetCopy(self, src, copy, targetNode):
        """
        :type src: TreeNode
        :type copy: TreeNode
        :type targetNode: TreeNode
        :rtype: TreeNode
        """
        # TODO

Examples:

  • Input: tree = [7,4,3,null,null,6,19], target = 3 → Output: 3
  • Input: tree = [7], target = 7 → Output: 7
  • Input: tree = [8,null,6,null,5,null,4,null,3,null,2,null,1], target = 4 → Output: 4

Constraints:

  • Number of nodes in the tree from 1 to 10^4
  • All values in the tree are unique
  • The target node definitely exists and is not null