(二叉树 BFS) 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
.
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 y
are 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:
- The number of nodes in the tree will be between
2
and100
. - Each node has a unique integer value from
1
to100
.
-----------------------------------------------------------------------------------------
额,这个题,怎么说的,弄懂了方法,就会发现这个题是比较简单的,是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的更多相关文章
- Leetcode之深度优先搜索(DFS)专题-199. 二叉树的右视图(Binary Tree Right Side View)
Leetcode之深度优先搜索(DFS)专题-199. 二叉树的右视图(Binary Tree Right Side View) 深度优先搜索的解题详细介绍,点击 给定一棵二叉树,想象自己站在它的右侧 ...
- 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 ...
- 【Leetcode_easy】993. Cousins in Binary Tree
problem 993. Cousins in Binary Tree 参考 1. Leetcode_easy_993. Cousins in Binary Tree; 完
- [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 ...
- 【LeetCode】993. Cousins in Binary Tree 解题报告(C++ & python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...
- (二叉树 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 ...
- 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 ...
- LeetCode.993-二叉树中的堂兄弟(Cousins in Binary Tree)
这是悦乐书的第374次更新,第401篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第235题(顺位题号是993).在二叉树中,根节点在深度0处,并且每个深度为k的节点的子 ...
- (二叉树 递归) 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 ...
随机推荐
- Android为TV端助力:(转载)修改TextView字体样式
一.开篇 因为 Android 字体相关的内容还比较多的.有时候其实我们只需要调整一下属性就可以满足设计师的需求,或者是一个退后的方案(毕竟有发版的时间卡住了),有一些效果可以大概满足需求. 那么本文 ...
- Android远程桌面助手(B1391)
ARDC(B1391), Download:https://files.cnblogs.com/files/we-hjb/ARDC%28B1391%29_EN.7z Android远程桌面助手(B13 ...
- Windows系统在本地配置一个apache域名的方法
我使用的xampp 1.修改C:\Windows\System32\drivers\etc中的hosts文件,添加127.0.0.1 www.feiquan.com 2.修改D:\xampp\apac ...
- 码农也来关注下经济问题<美元加息>对我们的影响
昨天凌晨三点,美联储宣布加息25个基点,这是今年美联储第四次加息,也是2015年12月份以来的第九次加息.基准利率又上调了25个基点,全球市场又要开始惴惴不安了. 要知道上一次美国基准利率上调25个基 ...
- C#基础知识之IOC
依赖注入:http://www.cnblogs.com/leoo2sk/archive/2009/06/17/1504693.html IOC:https://jinnianshilongnian.i ...
- zookeeper 分布式锁原理
zookeeper 分布式锁原理: 1 大家也许都很熟悉了多个线程或者多个进程间的共享锁的实现方式了,但是在分布式场景中我们会面临多个Server之间的锁的问题,实现的复杂度比较高.利用基于googl ...
- time-时间模块
# time模块import time# 时间戳res = time.time() # ***print(res, type(res)) # time.sleep(1) # ***# print(12 ...
- 2 OpenWrt路由器系统开发与网页设计
https://www.zhongkerd.com/news/content-729.html 摘 要: 目前商用WiFi路由器已应用到多个领域,商家通过给用户提供一个稳定免费WiFi热点达到吸引客户 ...
- 类Math
概述 java.lang.Math 类包含用于执行基本数学运算的方法,如初等指数.对数.平方根和三角函数.类似这样的工具 类,其所有方法均为静态方法,并且不会创建对象,调用起来非常简单. 常用方法 ...
- Fire Again CodeForces - 35C (BFS)
After a terrifying forest fire in Berland a forest rebirth program was carried out. Due to it N rows ...