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. Java thrift服务器和客户端创建实例

    首先环境介绍一下: 1.IntelliJ IDEA 2017.1 2.thrift-0.9.3 相信大家在看我这篇文章的时候已经对thrift通信框架已有所调研,这里就不再赘述了,直接进入正题: &l ...

  2. base64图片存储

    将图片转换为Base64编码,可以让你很方便地在没有上传文件的条件下将图片插入其它的网页.编辑器中. 这对于一些小的图片是极为方便的,因为你不需要再去寻找一个保存图片的地方. Base64编码在ora ...

  3. MyDAL - in && not in 条件 使用

    索引: 目录索引 一.API 列表 C# 代码中 接口 IList.Contains() 方法生成 SQL 对应的 in(val1,val2,... ...) 如:.Queryer<Agent& ...

  4. JPA实现复杂条件分页查询

    相信熟悉Hibernate的人对于ORM给编程带来的便利于快捷一定不陌生,相对于MyBatis等需要编写复杂的SQL语句,ORM映射为我们带来的便利显而易见.但是,在获得便利的同时,失去的便是灵活性, ...

  5. docker部署postgresql时,data目录不生效的问题探究

    今天用docker部署postgresql,用的是官方的镜像.结果挂载完 /var/lib/postgresql/data目录后,和容器里的目录其实并没有挂载成功. 母机上的目录并没有成功挂载到容器里 ...

  6. django连接sqlserver

    http://www.cnblogs.com/yijiaming/p/9684601.html 方法一: 1.需要安装pymssql pip install pymssql 2.使用方法: impor ...

  7. 公钥密码RSA算法记录

    介绍: RSA算法是1978年由 R.Rivest.A.Shamir.L.Adleman提出的一种用数论构造的.也是迄今为止理论上最为成熟.完善的公钥密码体,该体制已得到广泛的应用. 算法描述: 1. ...

  8. RHEL7/CentOS7 安装Zabbix

    1.添加 Zabbix 软件仓库 rpm -ivh http://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-1.el7.n ...

  9. centos7 最小安装初始化

    配置阿里yum源 mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup \&&cu ...

  10. ubuntu18.04修改网卡名称为eth0

    1.修改grub文件 vim /etc/default/grub 查找 GRUB_CMDLINE_LINUX="" 修改为 GRUB_CMDLINE_LINUX="net ...