[leetcode]1379. Find a Corresponding Node of a Binary Tree in a Clone of That Tree

链接

leetcode

描述

  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.

Example 1:

Input: tree = [7,4,3,null,null,6,19], target = 3

Output: 3

  Explanation: In all examples the original and cloned trees are shown. The target node is a green node from the original tree. The answer is the yellow node from the cloned tree.

Example 2:

Input: tree = [7], target = 7

Output: 7

Example 3:

Input: tree = [8,null,6,null,5,null,4,null,3,null,2,null,1], target = 4

Output: 4

Example 4:

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

Output: 5

Example 5:

Input: tree = [1,2,null,3], target = 2

Output: 2

Constraints:

The number of nodes in the tree is in the range [1, 10^4].

The values of the nodes of the tree are unique.

target node is a node from the original tree and is not null.

solution

  采用最朴素的BFS(广度优先遍历)即可;

class Solution {
public:
TreeNode* getTargetCopy(TreeNode* original, TreeNode* cloned, TreeNode* target) {
queue<TreeNode*> oq, cq;
if (original == NULL)
return NULL;
oq.push(original);
cq.push(cloned); while (!oq.empty())
{
TreeNode *tmp = oq.front();
TreeNode *tmpC = cq.front(); oq.pop();
cq.pop(); if (tmp == target)
return tmpC; if (tmp->left)
{
oq.push(tmp->left);
cq.push(tmpC->left);
} if (tmp->right)
{
oq.push(tmp->right);
cq.push(tmpC->right);
}
}
return NULL;
}
};

分析

  时间复杂度 O(n);n是二叉树中节点的个数,树种的每一个节点只会被遍历一次;

  空间复杂度O(1)

clik me github

personal page

[leetcode]1379. Find a Corresponding Node of a Binary Tree in a Clone of That Tree的更多相关文章

  1. LeetCode算法题-Second Minimum Node In a Binary Tree(Java实现)

    这是悦乐书的第285次更新,第302篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第153题(顺位题号是671).给定非空的特殊二叉树,其由具有非负值的节点组成,其中该树 ...

  2. 【LeetCode】671. Second Minimum Node In a Binary Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 找出所有值再求次小值 遍历时求次小值 日期 题目地址 ...

  3. Python 解LeetCode:671. Second Minimum Node In a Binary Tree

    题目在这里,要求一个二叉树的倒数第二个小的值.二叉树的特点是父节点的值会小于子节点的值,父节点要么没有子节点,要不左右孩子节点都有. 分析一下,根据定义,跟节点的值肯定是二叉树中最小的值,剩下的只需要 ...

  4. LeetCode题解之Second Minimum Node In a Binary Tree

    1.题目描述 2.问题分析 使用set. 3.代码 set<int> s; int findSecondMinimumValue(TreeNode* root) { dfs(root); ...

  5. 乘风破浪:LeetCode真题_019_Remove Nth Node From End of List

    乘风破浪:LeetCode真题_019_Remove Nth Node From End of List 一.前言 这次总算到了链表的操作了,之后肯定会有排序算法,二叉树,排序树,图等等的操作,现在我 ...

  6. LeetCode 671. 二叉树中第二小的节点(Second Minimum Node In a Binary Tree) 9

    671. 二叉树中第二小的节点 671. Second Minimum Node In a Binary Tree 题目描述 给定一个非空特殊的二叉树,每个节点都是正数,并且每个节点的子节点数量只能为 ...

  7. leetcode面试准备:Lowest Common Ancestor of a Binary Search Tree & Binary Tree

    leetcode面试准备:Lowest Common Ancestor of a Binary Search Tree & Binary Tree 1 题目 Binary Search Tre ...

  8. Leetcode之广度优先搜索(BFS)专题-133. 克隆图(Clone Graph)

    Leetcode之广度优先搜索(BFS)专题-133. 克隆图(Clone Graph) BFS入门详解:Leetcode之广度优先搜索(BFS)专题-429. N叉树的层序遍历(N-ary Tree ...

  9. 【LeetCode】1022. Sum of Root To Leaf Binary Numbers 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 日期 题目地址:https://leetco ...

随机推荐

  1. go micro实战01:快速搭建服务

    背景 go-micro给我们提供了一个非常便捷的方式来快速搭建微服务,而且并不需要提前系统了解micro,下面用一个简单的示例来快速实现一个服务. 创建Proto文件 因为我们要做微服务,那么就一定有 ...

  2. hdu1908 逆序对

    题目链接:https://www.luogu.com.cn/problem/P1908 这个题不要以为拿到手就可以树状数组秒,本题的数据范围是1e9显然简单的树状数组是空间不够的,点个数有5e5,所以 ...

  3. [STL] Codeforces 69E Subsegments

    Subsegments time limit per test 1 second memory limit per test 256 megabytes input standard input ou ...

  4. Redis 6.0 新增功能 - ACL

    Redis 6.0 ACL 期待已久的ACL终于来了,大家知道在redis集群中只有一个db,在多项目操作时可以通过key前缀来区分,但是还是能获取其它key,这样就带来了安全风险. Access C ...

  5. 从源码学习Java并发的锁是怎么维护内部线程队列的

    从源码学习Java并发的锁是怎么维护内部线程队列的 在上一篇文章中,凯哥对同步组件基础框架- AbstractQueuedSynchronizer(AQS)做了大概的介绍.我们知道AQS能够通过内置的 ...

  6. FastAI 简介

    Fastai简介 在深度学习领域,最受学生欢迎的MOOC课程平台有三个:Fast.ai.deeplearning.ai /Coursera和Udacity.Fastai作为其中之一,是一个课程平台,一 ...

  7. GitHub也会断供:美国制裁地区帐号都受限,毫无预警,个人页面直接404

    请注意,GitHub也有断供危机. 如果你有GitHub私有库,是时候重新思考安全性,也是时候制定备份策略. 这不是杞人忧天,也不只温馨提示,而是已经发生的事实. 一位伊朗程序员,一觉醒来GitHub ...

  8. kaggle入门——泰坦尼克之灾

    目录 引言 数据认识 总结 特征处理 建模预测 logistic分类模型 随机森林 SVM xgboost 模型验证 交叉验证 学习曲线 高偏差: 高方差 模型融合 总结 后记 引言 一直久闻kagg ...

  9. 服务器上安装.NET Framework 3.5 sp1

    操作系统是Windows Server 2008 R2 或 Windows Server 2012 或 Windows Server 2012 R2,可以直接进入“服务器管理器”添加“功能”.

  10. 线程 -- ThreadLocal

    1,ThreadLocal 不是“本地线程”的意思,而是Thread 的局部变量.每个使用该变量的线程提供独立的变量副本,所以每一个线程都可以独立地改变自己的副本,而不会影响其它线程所对应的副本 2, ...