1379. Find a Corresponding Node of a Binary Tree in a Clone of That Tree LeetCode Solution

In this guide, you will get 1379. Find a Corresponding Node of a Binary Tree in a Clone of That Tree LeetCode Solution with the best time and space complexity. The solution to Find a Corresponding Node of a Binary Tree in a Clone of That Tree problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Find a Corresponding Node of a Binary Tree in a Clone of That Tree solution in C++
  4. Find a Corresponding Node of a Binary Tree in a Clone of That Tree solution in Java
  5. Find a Corresponding Node of a Binary Tree in a Clone of That Tree solution in Python
  6. Additional Resources
1379. Find a Corresponding Node of a Binary Tree in a Clone of That Tree LeetCode Solution image

Problem Statement of Find a Corresponding Node of a Binary Tree in a Clone of That Tree

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.

Example 1:

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.

See also  659. Split Array into Consecutive Subsequences LeetCode Solution

Example 2:

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

Example 3:

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?

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(h)

1379. Find a Corresponding Node of a Binary Tree in a Clone of That Tree LeetCode Solution in C++

class Solution {
 public:
  TreeNode* getTargetCopy(TreeNode* original, TreeNode* cloned,
                          TreeNode* target) {
    TreeNode* ans = nullptr;
    dfs(original, cloned, target, ans);
    return ans;
  }

 private:
  void dfs(TreeNode* original, TreeNode* cloned, TreeNode* target,
           TreeNode*& ans) {
    if (ans != nullptr)
      return;
    if (original == nullptr)
      return;
    if (original == target) {
      ans = cloned;
      return;
    }
    dfs(original->left, cloned->left, target, ans);
    dfs(original->right, cloned->right, target, ans);
  }
};
/* code provided by PROGIEZ */

1379. Find a Corresponding Node of a Binary Tree in a Clone of That Tree LeetCode Solution in Java

class Solution {
  public final TreeNode getTargetCopy(final TreeNode original, final TreeNode cloned,
                                      final TreeNode target) {
    dfs(original, cloned, target);
    return ans;
  }

  private TreeNode ans = null;

  private void dfs(TreeNode original, TreeNode cloned, TreeNode target) {
    if (ans != null)
      return;
    if (original == null)
      return;
    if (original == target) {
      ans = cloned;
      return;
    }
    dfs(original.left, cloned.left, target);
    dfs(original.right, cloned.right, target);
  }
}
// code provided by PROGIEZ

1379. Find a Corresponding Node of a Binary Tree in a Clone of That Tree LeetCode Solution in Python

class Solution:
  def getTargetCopy(
      self,
      original: TreeNode,
      cloned: TreeNode,
      target: TreeNode,
  ) -> TreeNode:
    ans = None

    def dfs(original: TreeNode, cloned: TreeNode) -> None:
      nonlocal ans
      if ans:
        return
      if not original:
        return
      if original == target:
        ans = cloned
        return

      dfs(original.left, cloned.left)
      dfs(original.right, cloned.right)

    dfs(original, cloned)
    return ans
# code by PROGIEZ

Additional Resources

See also  3175. Find The First Player to win K Games in a Row LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.