[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. linux入门系列18--Web服务之Apache服务2

    接上一篇文章,在了解Apache基本配置以及SELinux相关知识后,继续演示Apache提供的虚拟主机功能以及访问控制方式. 如果还没看上一篇的建议先查看后再来,上篇文章"linux入门系 ...

  2. 零售CRM系统开发的核心功能

    在零售行业中,客户关系管理系统是一个包含销售,市场营销和客户服务流程的中央枢纽.它为企业所有者提供了一种可以结合所有与销售有关的问题并管理销售流程的有效工具.零售CRM可以留住客户,提供个性化的一流客 ...

  3. Log4j模板

    log4j.rootLogger=DEBUG, A1,A2 log4j.appender.A1.MaxFileSize=1kb #10个备份 log4j.appender.A1.MaxBackupIn ...

  4. POJ1270 toposort+DFS+回溯

    题目链接:http://poj.org/problem?id=1270 这道题其实就是求所有满足条件的topo序,我们考虑到给定的字符是确定的,也就是他们的长度都是一样的,所以为了得到所有的情况,我们 ...

  5. Java井字棋游戏

    试着写了一个井字棋游戏,希望各位能给予一些宝贵的建议. 一.棋盘类 package 井字棋游戏; public class ChessBoard { private int number; Perso ...

  6. Git提交代码的流程——新手适用

    pull:是下拉代码,相等于将远程的代码下载到你本地,与你本地的代码合并push:是推代码,将你的代码上传到远程的动作完整的流程是: 第一种方法:(简单易懂) 1.git add .(后面有一个点,意 ...

  7. Java GC(垃圾回收)机制知识总结

    目录 Java GC系列 Java关键术语 Java HotSpot 虚拟机 JVM体系结构 Java堆内存 启动Java垃圾回收 Java垃圾回收过程 垃圾回收中实例的终结 对象什么时候符合垃圾回收 ...

  8. python 产生随机数,随机字符串

    import randomimport string#随机整数:print random.randint(1,50)#随机选取0到100间的偶数:print random.randrange(0, 1 ...

  9. msys2 mingw64安装

    (1)安装msys2 (2)更新\etc\pacman.d\下的源文件 mirrorlist.msys Server = http://repo.msys2.org/msys/$arch/ Serve ...

  10. JavaScript语法记要

    JavaScript语法记要 1.JS代码忽略缩进和换行 2.JS六种数据类型 String // 字符串 Number // 数值 Boolean // 布尔值 null // 空值 undefin ...