【算法】1379. 找出克隆二叉树中的相同节点(多语言实现)

非常感谢你阅读本文~
欢迎【?点赞】【⭐收藏】【?评论】~
放弃不难,但坚持一定很酷~
希望我们大家都能每天进步一点点~
本文由 二当家的白帽子:https://le-yi.blog.csdn.net/ 博客原创~



1379. 找出克隆二叉树中的相同节点:

给你两棵二叉树,原始树 original 和克隆树 cloned,以及一个位于原始树 original 中的目标节点 target

其中,克隆树 cloned 是原始树 original 的一个 副本

请找出在树 cloned 中,与 target 相同 的节点,并返回对该节点的引用(在 C/C++ 等有指针的语言中返回 节点指针,其他语言返回节点本身)。

注意:

  1. 不能 对两棵二叉树,以及 target 节点进行更改。
  2. 只能 返回对克隆树 cloned 中已有的节点的引用。

进阶:如果树中允许出现值相同的节点,你将如何解答?

样例 1:

在这里插入图片描述

输入: 
	tree = [7,4,3,null,null,6,19], target = 3
	
输出: 
	3
	
解释: 
	上图画出了树 original 和 cloned。target 节点在树 original 中,用绿色标记。答案是树 cloned 中的黄颜色的节点(其他示例类似)。

样例 2:

在这里插入图片描述

输入: 
	tree = [7], target =  7
	
输出: 
	7

样例 3:

在这里插入图片描述

输入: 
	tree = [8,null,6,null,5,null,4,null,3,null,2,null,1], target = 4
	
输出: 
	4

样例 4:

在这里插入图片描述

输入: 
	tree = [1,2,3,4,5,6,7,8,9,10], target = 5
	
输出: 
	5

样例 5:

在这里插入图片描述

输入: 
	tree = [1,2,null,3], target = 2
	
输出: 
	2

提示:

  • 树中节点的数量范围为 [1, 104]
  • 同一棵树中,没有值相同的节点。
  • target 节点是树 original 中的一个节点,并且不会是 null

分析

  • 这道算法题的需要简单翻译一下,三个参数:第一个参数 original 是原树;第二个参数 cloned 是第一个参数的克隆拷贝;第三个参数 target 是我们要找到的节点,它是第一个参数 original 中的一个节点,需要找到并返回第二个参数 cloned 里对应的节点。
  • 提示中说同一棵树中,没有值相同的节点。所以有的小伙伴可能觉得第一个参数非常多余,二当家也这么觉得。我们直接遍历第二个参数 cloned ,直到找到和第三个参数 target 值相同的节点并返回就可以了。
  • 其实第一个参数在 进阶 挑战里就有用了,如果树中允许出现值相同的节点,那就不能用值去判断是相同节点了。
  • 这时候就需要用到第一个参数 original ,因为第三个参数 target 是原树中的一个节点,所以我们可以直接根据地址判断是否是相同节点。
  • 第二个参数 cloned 是第一个参数的克隆拷贝,所以它们具有相同结构,我们只要按照相同顺序同时遍历原树和克隆树,就可以找到答案。

题解

java

非递归遍历

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */

class Solution {
    public final TreeNode getTargetCopy(final TreeNode original, final TreeNode cloned, final TreeNode target) {
        Deque<TreeNode> stack      = new LinkedList<>();
        TreeNode        node       = original;
        TreeNode        clonedNode = cloned;
        while (node != null || !stack.isEmpty()) {
            if (node != null) {
                if (node == target) {
                    return clonedNode;
                }
                stack.push(clonedNode);
                stack.push(node);
                node = node.left;
                clonedNode = clonedNode.left;
            } else {
                node = stack.pop().right;
                clonedNode = stack.pop().right;
            }
        }
        return null;
    }
}

递归遍历

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */

class Solution {
    public final TreeNode getTargetCopy(final TreeNode original, final TreeNode cloned, final TreeNode target) {
        if (cloned == null
                || original == target) {
            return cloned;
        }
        TreeNode ans = getTargetCopy(original.left, cloned.left, target);
        if (ans == null) {
            ans = getTargetCopy(original.right, cloned.right, target);
        }
        return ans;
    }
}

c++

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */

class Solution {
public:
    TreeNode* getTargetCopy(TreeNode* original, TreeNode* cloned, TreeNode* target) {
        if (cloned == nullptr
            || original == target) {
            return cloned;
        }
        TreeNode* ans = getTargetCopy(original->left, cloned->left, target);
        if (ans == nullptr) {
            ans = getTargetCopy(original->right, cloned->right, target);
        }
        return ans;
    }
};

python

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

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

在这里插入图片描述


原题传送门:https://leetcode-cn.com/problems/find-a-corresponding-node-of-a-binary-tree-in-a-clone-of-that-tree/


本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇
下一篇>>