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. js 滚轮控制图片缩放大小和拖动

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. 超级有爱的五款APP共享 可以让你神操作

    随着科技的不断发展,手机功能的不断完善,让我们更加依赖手机,不得不说手机给我们带来很多的乐趣和方便. 今天就主要给大家分享五款超级有爱的APP软件,感兴趣的小伙伴已经迫不及待了吧! 荔枝 荔枝是一款声 ...

  3. 软件工程实践-WC项目之C实现

    1.Github项目地址 https://github.com/ShadowEvan/homework 基本功能 -c 统计文件字符数(实现) -w 统计文件词数(实现) -l  统计文件行数(实现) ...

  4. spring笔记----看书笔记

    上周末看了一章以前javaee轻量级的书spring部分,简单做了一些笔记 // ApplicationContext ac=new ClassPathXmlApplicationContext(&q ...

  5. 【转】Python之道

    作者:Vamei 出处:http://www.cnblogs.com/vamei Python有一个彩蛋,用下面语句调出: import this 该彩蛋的文档记录于PEP 20. 语句执行之后,终端 ...

  6. python使用rabbitMQ介绍五(话题模式)

    一.模式介绍 话题模式(Topic)基本思想和路由模式是一样的,只不过路由键支持模糊匹配,符号“#”匹配一个或多个词,符号“*”匹配不多不少一个词 话题模式相当于消息的模糊匹配,或者按照正则匹配.其中 ...

  7. Asp.Net中virtual、override理解

    virtual关键字用于指定属性或方法在派生类中重写.默认情况下,派生类从其基类继承属性和方法,如果继承的属性或方法需要在派生类中有不同的行为,则可以重写它,即可以在派生类中定义该属性或方法的新实现, ...

  8. 想知道谁是你的最佳用户?基于Redis实现排行榜周期榜与最近N期榜

    本文由云+社区发表 前言 业务已基于Redis实现了一个高可用的排行榜服务,长期以来相安无事.有一天,产品说:我要一个按周排名的排行榜,以反映本周内用户的活跃情况.于是周榜(按周重置更新的榜单)诞生了 ...

  9. drools规则引擎与kie-wb和kie-server远程执行规则(7.18.0.Final)

    最近研究了一下规则引擎drools. 这篇博客带你搭建并运行一个可在线编辑,在线打包,远程执行的规则引擎(drools) 本篇博客同时参考https://blog.csdn.net/chinrui/a ...

  10. 练习 python之数据库增删改查

    # 文件存储时可以这样表示 ## id,name,age,phone,dept,enroll_date# 1,Alex Li,22,13651054608,IT,2013-04-01# 2,Jack ...