列表

详情


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

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

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

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

 

注意:不能 对两棵二叉树,以及 target 节点进行更改。只能 返回对克隆树 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

 

提示:

 

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

原站题解

去查看

上次编辑到这里,代码来自缓存 点击恢复默认模板
/** * 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) { } };

cpp 解法, 执行用时: 388 ms, 内存消耗: 160 MB, 提交时间: 2024-04-03 00:07:16

/**
 * 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(!original)
            return nullptr;
        if(original!=target)//如果不等于,分别遍历左和右
        {
            TreeNode* a=getTargetCopy(original->left,cloned->left,target);
            if(a)//如果a是nullptr,就说明original->left中没有target
                return a;
            TreeNode*b=getTargetCopy(original->right,cloned->right,target);
            if(b)
                return b;
            return nullptr;
        }
        else
            return cloned;//如果等于,直接返回
    }
};

java 解法, 执行用时: 1 ms, 内存消耗: 49.2 MB, 提交时间: 2022-11-12 13:02:42

/**
 * 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 (original == target) {
			return cloned;
		}
		TreeNode ans = null;
		if (original.left != null) {
			ans = this.getTargetCopy(original.left, cloned.left, target);
		}
		if (ans == null && original.right != null) {
			return this.getTargetCopy(original.right, cloned.right, target);
		}
		return ans;
	}
}

python3 解法, 执行用时: 1152 ms, 内存消耗: 25.3 MB, 提交时间: 2022-11-12 13:01:40

# 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:
        dic = dict()
        def preorder(cloned: TreeNode) -> None:
            nonlocal dic
            if not cloned:
                return
            dic[cloned.val] = cloned
            preorder(cloned.left)
            preorder(cloned.right)
            
        preorder(cloned)
        return dic[target.val]

上一题