In a binary tree, the root node is at depth 0, and children of each depth knode are at depth k+1.

Two nodes of a binary tree are cousins if they have the same depth, but have different parents.

We are given the root of a binary tree with unique values, and the values x and y of two different nodes in the tree.

Return true if and only if the nodes corresponding to the values x and yare cousins.

Example 1:

Input: root = [1,2,3,4], x = 4, y = 3
Output: false

Example 2:

Input: root = [1,2,3,null,4,null,5], x = 5, y = 4
Output: true

Example 3:

Input: root = [1,2,3,null,4], x = 2, y = 3
Output: false

Note:

  1. The number of nodes in the tree will be between 2 and 100.
  2. Each node has a unique integer value from 1 to 100.

-----------------------------------------------------------------------------------------

额,这个题,怎么说的,弄懂了方法,就会发现这个题是比较简单的,是easy题是有道理的。要好好扎实BFS的基础。

这个题就是先把每个结点的父节点和它的位置求出,然后再比较,可以用hash。

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:
bool isCousins(TreeNode* root, int x, int y) {
queue<pair<TreeNode*,TreeNode*> > q;
q.push(make_pair(root,nullptr));
int depth = ;
unordered_map<int,pair<TreeNode*,int> > mp; //TreeNode*指的是这个结点的父结点。第二个int指的是深度。
while(!q.empty()){
for(int i = q.size(); i > ; i--){
auto t = q.front();
q.pop();
mp[t.first->val] = make_pair(t.second,depth);
if(t.first->left) q.push(make_pair(t.first->left,t.first));
if(t.first->right) q.push(make_pair(t.first->right,t.first));
}
depth++;
}
auto tx = mp[x],ty = mp[y];
return tx.first != ty.first && tx.second == ty.second;
}
};

(二叉树 BFS) leetcode993. Cousins in Binary Tree的更多相关文章

  1. Leetcode之深度优先搜索(DFS)专题-199. 二叉树的右视图(Binary Tree Right Side View)

    Leetcode之深度优先搜索(DFS)专题-199. 二叉树的右视图(Binary Tree Right Side View) 深度优先搜索的解题详细介绍,点击 给定一棵二叉树,想象自己站在它的右侧 ...

  2. LeetCode 993. Cousins in Binary Tree(判断结点是否为Cousin)

    993. Cousins in Binary Tree In a binary tree, the root node is at depth 0, and children of each dept ...

  3. 【Leetcode_easy】993. Cousins in Binary Tree

    problem 993. Cousins in Binary Tree 参考 1. Leetcode_easy_993. Cousins in Binary Tree; 完

  4. [Swift]LeetCode993. 二叉树的堂兄弟节点 | Cousins in Binary Tree

    In a binary tree, the root node is at depth 0, and children of each depth k node are at depth k+1. T ...

  5. 【LeetCode】993. Cousins in Binary Tree 解题报告(C++ & python)

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

  6. (二叉树 BFS) leetcode513. Find Bottom Left Tree Value

    Given a binary tree, find the leftmost value in the last row of the tree. Example 1: Input: 2 / \ 1 ...

  7. LeetCode 993 Cousins in Binary Tree 解题报告

    题目要求 In a binary tree, the root node is at depth 0, and children of each depth k node are at depth k ...

  8. LeetCode.993-二叉树中的堂兄弟(Cousins in Binary Tree)

    这是悦乐书的第374次更新,第401篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第235题(顺位题号是993).在二叉树中,根节点在深度0处,并且每个深度为k的节点的子 ...

  9. (二叉树 递归) leetcode 889. Construct Binary Tree from Preorder and Postorder Traversal

    Return any binary tree that matches the given preorder and postorder traversals. Values in the trave ...

随机推荐

  1. 剑指前端(前端入门笔记)——Date类型

    Date类型 ECMAScript中的Date类型是在早期Java中的java.util.Date类基础上构建的.为此,Date类型使用自UTC(国际协调时间)1970年1月1日午夜(零时)开始经过的 ...

  2. H5、C3、ES6的新特性

    H5的新特性 1.语义化标签 有利于SEO,有助于爬虫抓取更多的有效信息,爬虫是依赖于标签来确定上下文和各个关键字的权重. 语义化的HTML在没有CSS的情况下也能呈现较好的内容结构与代码结构 方便其 ...

  3. dede首页、列表页调用非缩略图

    在include/extend.func.php末尾添加 function firstimg($str_pic) { $str_sub=substr($str_pic,0,-7).strrchr($s ...

  4. 一篇文章了解Github和Git教程-AndroidStudio上传Github教程

    前言 为了方便保存自己的代码,下班后可以回家继续进行,自己的码农工作,介绍一下Github. 什么是Github呢? 作为一个编程人员,我觉得得了解一下Github吧! 当然,如果你放弃了码农或者技术 ...

  5. 网页验证码出不来,读取验证码时出错:javax.imageio.IIOException: Can't create cache file!

    版权声明:本文为博主原创文章,仅作为学习交流使用 转载请注明出处 https://www.cnblogs.com/linck/p/10593053.html 今天打开项目时,发现登陆界面的验证码出不来 ...

  6. R语言学习——图形初阶之散点图

    使用R内置的数据框mtcars,绘制车身重量与每加仑汽油行驶的英里数的散点图,要求横轴为车身重量(wt),纵轴为每加仑汽油行驶的英里数(mpg),并添加最优拟合曲线.标题,输出为pdf文件.代码实现如 ...

  7. 试试Linux下的ip命令,ifconfig已经过时了

    linux的ip命令和ifconfig类似,但前者功能更强大,并旨在取代后者.使用ip命令,只需一个命令,你就能很轻松地执行一些网络管理任务.ifconfig是net-tools中已被废弃使用的一个命 ...

  8. Django学习笔记(3)--模板

    模板 在实际的页面大多是带样式的HTML代码,而模板是一种带有特殊语法的html文件,这个html文件可以被django编译,可以传递参数进去, 实现数据动态化.在编译完成后,生成一个普通的html文 ...

  9. MySQL二进预编译制安装

    +++++++++++++++++++++++++++++++++++++++++++标题:MySQL二进预编译制安装时间:2019年2月25日内容:MySQL二进制预编译安装重点:MySQL二进制预 ...

  10. 译注(2): How to Write a 21st Century Proof

    原文:Computer Scientist Tells Mathematicians How To Write Proofs 对比一下下面两个证明哪个更好? 版本一: "A square a ...