File: find-a-corresponding-node-of-a-binary-tree-in-a-clone-of-that-tree/solution.py

Date: 2026-06-06

Time: 16:32

Purpose

This file solves LeetCode 1379: Find a Corresponding Node of a Binary Tree in a Clone of That Tree. Given an original binary tree, a structurally identical clone, and a reference to a node in the original, it finds and returns the reference to the corresponding node in the clone.

The file is self-contained: it defines the TreeNode class, the solution function, and a full test suite.

Key Components

TreeNode (lines 7-10)

Standard binary tree node with val, left, right. Used by both the solution and tests.

getTargetCopy(original, cloned, target) -> TreeNode (lines 13-27)

The core algorithm. Walks both trees in lockstep using the original tree to locate the target by identity (not value), then returns the corresponding node from the cloned tree.

Contract:

TestGetTargetCopy (lines 32-87)

Six test cases covering: interior node, single-node tree, right-skewed chain, root as target, leaf node, left-skewed chain.

Patterns

Parallel recursive traversal. The key insight is traversing original and cloned simultaneously. The identity check (original is target) happens on the original tree, but the *return value* comes from the cloned tree. This avoids value-based comparison, which matters when duplicate values exist.

Short-circuit DFS. The left subtree result is checked before recursing right (lines 25-27). If the target is found in the left subtree, the right subtree is never visited. This is a standard optimization for "find one node" tree problems.

clone helper in tests. Tests build the original tree by hand, then clone it with clone, and verify that the result is both value-correct (assertEqual) and identity-correct (assertIs).

Dependencies

Imports: typing.Optional (declared but unused in the function signature — the actual return type annotation uses TreeNode directly), unittest.

Imported by: The "Imported By" list in the prompt is misleading — it lists hundreds of unrelated test files. The only real consumer is find-a-corresponding-node-of-a-binary-tree-in-a-clone-of-that-tree/test_solution.py.

Flow

1. Enter getTargetCopy with roots of both trees and the target reference.

2. Check original is target — if yes, return the corresponding cloned node. This is the base case that produces the result.

3. Check original is None — if yes, return None. This is the base case that prevents walking off the tree.

4. Recurse left. If the left subtree returned a non-None result, propagate it up immediately.

5. Otherwise, recurse right and return whatever it produces (either the found node or None).

The recursion is pre-order DFS. Worst case (target is the rightmost leaf) visits every node — O(n) time, O(h) stack space where h is tree height.

Invariants

Error Handling

None. The function trusts its inputs completely — no validation, no exceptions. A None return implicitly signals "target not found," but the problem guarantees the target exists, so this path shouldn't occur in valid usage.

Topics to Explore

Beliefs