(二叉树 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端助力:自定义view之太阳
先看效果图 package com.hhzt.iptv.lvb_w8.view; import android.content.Context;import android.graphics.Canv ...
- Android Fragment碎片
什么是碎片? 碎片(Fragment)是一种可以嵌入在活动当中的UI片段,它能让程序更加合理和充分地利用大屏幕的空间,因而在平板上应用的非常广泛.可以把Fragment当成Activity一个界面的一 ...
- Maven deploy部署jar包到远程私仓
Maven deploy部署jar包到远程私仓 maven deploy介绍 maven中的仓库分为两种,snapshot快照仓库和release发布仓库.snapshot快照仓库用于保存开发过程中的 ...
- maven pom 属性介绍
maven pom属性 内置属性(预定义,可直接使用) ${basedir} 表示项目根目录,即包含pom.xml文件的目录; ${version} 表示项目版本; ${project.basedir ...
- MyDAL - .UpdateAsync() 之 .Set() 使用
索引: 目录索引 一.API 列表 1.Set<M, F>(Expression<Func<M, F>> propertyFunc, F newVal) 如: .S ...
- Linux CentOS 6 解决 Device eth0 does not seem to be present
一.故障现象: [root@c1node01 ~]# service network restart Shutting down loopback insterface: ...
- python接口自动化-session_自动发文
一.session简介 查看 requests.session() 帮助文档(只贴了一部分内容) import requests help(requests.session()) class Sess ...
- windows本地远程虚拟机docker中的数据的问题
关闭各种防火墙 打开宿主机(windows)的cmd,在其中添加通往192.168.1.0/24网络的路由. 通往192.168.1.0/24网络的数据包由172.20.1.12来转发 route a ...
- Oracle硬解析,软解析,软软解析介绍
Oracle数据库中的CURSOR分为两种类型:Shared Cursor 和 Session Cursor 1,Shared Cursor Oracle里的第一种类型的Cursor就是Shared ...
- BZOJ 1171: 大sz的游戏
ZJOI讲课的题目,数据结构什么的还是很友好的说 首先我们发现题目中提到的距离\(\le L\)的东西显然可以用单调队列维护 但是暴力搞去不掉区间并的限制,那么我们考虑从区间并入手 对于这种问题的套路 ...