• 【LeetCode每日一题】——1379.找出克隆二叉树中的相同节点


    一【题目类别】

    • 广度优先搜索

    二【题目难度】

    • 简单

    三【题目编号】

    • 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

    七【题目提示】

    • 树中节点的数量范围为 [ 1 , 1 0 4 ] 。 树中节点的数量范围为 [1, 10^4] 。 树中节点的数量范围为[1,104]
    • 同一棵树中,没有值相同的节点。 同一棵树中,没有值相同的节点。 同一棵树中,没有值相同的节点。
    • t a r g e t 节点是树 o r i g i n a l 中的一个节点,并且不会是 n u l l 。 target 节点是树 original 中的一个节点,并且不会是 null 。 target节点是树original中的一个节点,并且不会是null

    八【题目进阶】

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

    九【解题思路】

    • 此题没有难度,直接在克隆后的二叉树中进行广度优先搜索(BFS)target节点即可
    • 如果最后找到了,返回克隆后的二叉树中的target节点
    • 如果找不到,返回空

    十【时间频度】

    • 时间复杂度: O ( n ) O(n) O(n) n n n为传入的二叉树的节点个数
    • 空间复杂度: O ( n ) O(n) O(n) n n n为传入的二叉树的节点个数

    十一【代码实现】

    1. 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) {
            return bfs(cloned, target);    
        }
    
        public TreeNode bfs(TreeNode root, TreeNode target){
            Queue<TreeNode> queue = new LinkedList<>();
            queue.add(root);
            while(!queue.isEmpty()){
                TreeNode temp = queue.poll();
                if(temp.val == target.val){
                    return temp;
                }
                if(temp.left != null){
                    queue.add(temp.left);
                }
                if(temp.right != null){
                    queue.add(temp.right);
                }
            }
            return null;
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    1. 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:
            return self.bfs(cloned, target)
        
        def bfs(self, root: TreeNode, target: TreeNode) -> TreeNode:
            queue = [root]
            while queue:
                temp = queue.pop(0)
                if temp.val == target.val:
                    return temp
                if temp.left:
                    queue.append(temp.left)
                if temp.right:
                    queue.append(temp.right)
            return None
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    1. 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) {
            return bfs(cloned, target);
        }
    
        TreeNode* bfs(TreeNode* root, TreeNode* target){
            queue<TreeNode*> q;
            q.push(root);
            while(!q.empty()){
                struct TreeNode* temp = (struct TreeNode*)malloc(sizeof(struct TreeNode));
                temp = q.front();
                q.pop();
                if(temp->val == target->val){
                    return temp;
                }
                if(temp->left != NULL){
                    q.push(temp->left);
                }
                if(temp->right != NULL){
                    q.push(temp->right);
                }
            }
            return NULL;
        }
    
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    十二【提交结果】

    1. Java语言版
      在这里插入图片描述

    2. Python语言
      在这里插入图片描述

    3. C++语言版
      在这里插入图片描述

  • 相关阅读:
    React Router 6 快速上手
    Python绘图入门:使用Matplotlib绘制柱状图
    举个栗子~Minitab 技巧(3):用分组条形图快速对比数据
    VsCode Ctrl+.修复无效
    vue3入门(总结)
    Spring Cloud CLI简介
    attempt to compare nil with number -- 黑马点评出现问题
    Spring的ioc的扫描器与bean的作用域与生命周期
    2-1线性表-顺序表
    连夜解决 maven 阿里云镜像无法下载 gexin-rp-* 的问题
  • 原文地址:https://blog.csdn.net/IronmanJay/article/details/134005134