Skip to content

Latest commit

 

History

History
106 lines (70 loc) · 4.01 KB

071.md

File metadata and controls

106 lines (70 loc) · 4.01 KB

Difficulty: 🟢 Easy

Given two binary trees original and cloned and given a reference to a node target in the original tree. The cloned tree is a copy of the original tree. Return a reference to the same node in the cloned tree.

Note that you are not allowed to change any of the two trees or the target node and the answer must be a reference to a node in the cloned tree.

Examples:

Example 1:

071_01.png

Input: tree = [7,4,3,null,null,6,19], target = 3
Output: 3
Explanation: In all examples the original and cloned trees are shown. The target node is a green node from the original tree. The answer is the yellow node from the cloned tree.

Example 2:

071_02.png

Input: tree = [7], target =  7
Output: 7

Example 3:

071_03.png

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

Constraints:

  • The number of nodes in the tree is in the range [1, 104].
  • The values of the nodes of the tree are unique.
  • target node is a node from the original tree and is not null.

Follow up:

  • Could you solve the problem if repeated values on the tree are allowed?

Solutions

O(n) solution

** Python3

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

class Solution:
    def getTargetCopy(self, original: TreeNode, cloned: TreeNode, target: TreeNode) -> TreeNode:
        stack = [(original, cloned)]
        while stack:
            node1, node2 = stack.pop()
            print(node1.val, node2.val)
            if node1 is target:
                return node2

            if node1.right and node2.right:
                stack.append((node1.right, node2.right))

            if node1.left and node2.left:
                stack.append((node1.left, node2.left))

The given solution provides an iterative approach to finding the corresponding node in the cloned tree for a given node in the original tree. It uses a stack to perform a depth-first traversal of both trees simultaneously.

Here is a step-by-step overview of the solution:

  1. Initialize a stack with a tuple containing the root nodes of the original and cloned trees: (original, cloned).
  2. Enter a loop that continues until the stack is not empty.
  3. Pop a tuple (node1, node2) from the stack.
  4. Check if node1 is the target node. If it is, return node2 as the answer, which is the corresponding node in the cloned tree.
  5. If node1 has a right child and node2 has a corresponding right child, push a tuple containing these right children onto the stack.
  6. If node1 has a left child and node2 has a corresponding left child, push a tuple containing these left children onto the stack.
  7. Repeat steps 3-6 until the target node is found or all nodes are traversed.

Complexity Analysis

The time complexity for this solution is O(n), where n is the number of nodes in the binary tree. In the worst case, we may need to visit all nodes in both the original and cloned trees.

The space complexity is O(h), where h is the height of the binary tree. In the worst case, the stack can contain all nodes along the path from the root to the target node.

Summary

The given solution provides an iterative approach to finding the corresponding node in the cloned binary tree for a given node in the original tree. It uses a depth-first traversal with a stack to explore both trees simultaneously. The solution has a time complexity of O(n) and a space complexity of O(h), where n is the number of nodes and h is the height of the binary tree.

NB: If you want to get community points please suggest solutions in other languages as merge requests.