LeetCode 1379 Find a Corresponding Node of a Binary Tree in a Clone of That Tree (Python)

Posted by 小明MaxMing on January 2, 2020

题目

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.

解题思路

使用前序遍历同时递归两棵树,当在origin找到目标节点时,返回cloned树中对应节点

代码

class Solution:
    def getTargetCopy(self, original: TreeNode, cloned: TreeNode, target: TreeNode) -> TreeNode:
        if original == target:
            return cloned
        if original.left:
            res = self.getTargetCopy(original.left, cloned.left, target)
            if res:
                return res
        if original.right:
            res = self.getTargetCopy(original.right, cloned.right, target)
            if res:
                return res

视频讲解 YouTube<--欢迎点击订阅

视频讲解 bilibili<--欢迎点击订阅