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

你的名字 2022-10-06 10:49 117阅读 0赞

Topic

  • Tree
  • Depth-first Search
  • Breadth-first Search
  • Recursion

Description

https://leetcode.com/problems/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.

Follow up: Solve the problem if repeated values on the tree are allowed.

Example 1:

4f3f0ce0eae18411919b5680fe461adb.png

  1. Input: tree = [7,4,3,null,null,6,19], target = 3
  2. Output: 3
  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:

a0ad04ad2a7826e8d1ad8cec7a3e1492.png

  1. Input: tree = [7], target = 7
  2. Output: 7

Example 3:

f8776dbe2eb2cdf2c39c03b93462e3a0.png

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

Example 4:
9e904078cc60aea9f95fda44ef215018.png

  1. Input: tree = [1,2,3,4,5,6,7,8,9,10], target = 5
  2. Output: 5

Example 5:

df30e5dd767aebd29943ad1e862902cb.png

  1. Input: tree = [1,2,null,3], target = 2
  2. Output: 2

Constraints:

  • The number of nodes in the tree is in the range [ 1 , 1 0 4 ] [1, 10^4] [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.

Analysis

BFS,DFS都行,这题应为Easy难度。

Submission

  1. import java.util.LinkedList;
  2. import com.lun.util.BinaryTree.TreeNode;
  3. public class FindACorrespondingNodeOfABinaryTreeInACloneOfThatTree {
  4. public final TreeNode getTargetCopy(final TreeNode original, final TreeNode cloned, final TreeNode target) {
  5. LinkedList<TreeNode> stack = new LinkedList<>();
  6. stack.push(cloned);
  7. while(!stack.isEmpty()) {
  8. TreeNode node = stack.pop();
  9. if(node.val == target.val)
  10. return node;
  11. if(node.left != null)
  12. stack.push(node.left);
  13. if(node.right != null)
  14. stack.push(node.right);
  15. }
  16. return null;
  17. }
  18. }

Test

  1. import static org.junit.Assert.*;
  2. import org.junit.Test;
  3. import com.lun.util.BinaryTree;
  4. import com.lun.util.BinaryTree.TreeNode;
  5. public class FindACorrespondingNodeOfABinaryTreeInACloneOfThatTreeTest {
  6. @Test
  7. public void test() {
  8. FindACorrespondingNodeOfABinaryTreeInACloneOfThatTree obj = new FindACorrespondingNodeOfABinaryTreeInACloneOfThatTree();
  9. TreeNode root1 = BinaryTree.integers2BinaryTree(7,4,3,null,null,6,19);
  10. TreeNode clone1 = BinaryTree.integers2BinaryTree(7,4,3,null,null,6,19);
  11. assertEquals(3, obj.getTargetCopy(root1, clone1, new TreeNode(3)).val);
  12. }
  13. }

发表评论

表情:
评论列表 (有 0 条评论,117人围观)

还没有评论,来说两句吧...

相关阅读