Leetcode Problem 1379. Find a Corresponding Node of a Binary Tree in a Clone of That Tree

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

Leetcode Solutions

DFS: Recursive Inorder Traversal

  1. Start at the root of both the original and cloned trees.
  2. If the current node in the original tree is null, return null as there is nothing to compare.
  3. Recursively traverse the left subtree. If the recursive call returns a non-null value, it means the target has been found, so return this value.
  4. Check if the current node in the original tree is the target node. If it is, return the corresponding node in the cloned tree.
  5. If the target is not found yet, recursively traverse the right subtree.
  6. If the target is found in the right subtree, return the corresponding node from the cloned tree.
UML Thumbnail

BFS: Iterative Traversal

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...