1379. Find a Node of a Binary Tree in a Clone Tree
Question
Given two binary trees
originalandclonedand given a reference to a nodetargetin the original tree.The
clonedtree is a copy of theoriginaltree.Return a reference to the same node in the
clonedtree.Note that you are not allowed to change any of the two trees or the
targetnode and the answer must be a reference to a node in theclonedtree.
Solution
DFS搜索,同步递归original和cloned的子节点。
当在original里找到target的时候返回当前的cloned节点。
注意可以先判断一个分支中的返回值是否为空,如果不为空则直接返回。反之则返回另一个分支。这样操作可以进行一部分剪枝。
Code
1 | /** |
1379. Find a Node of a Binary Tree in a Clone Tree
https://xuanhe95.github.io/2022/05/18/1379-Find-a-Node-of-a-Binary-Tree-in-a-Clone-Tree/
