public class Node
{
public int CurNode;
public int FatherNode;
public int Layer;
} public class Solution
{
public List<Node> L = new List<Node>();
public void FrontTraversal(TreeNode root, int layer = )
{
if (root != null)
{
if (!L.Any())
{
L.Add(new Node() { CurNode = root.val, FatherNode = , Layer = layer });
}
if (root.left != null)
{
L.Add(new Node() { CurNode = root.left.val, FatherNode = root.val, Layer = layer + });
FrontTraversal(root.left, layer + );
}
if (root.right != null)
{
L.Add(new Node() { CurNode = root.right.val, FatherNode = root.val, Layer = layer + });
FrontTraversal(root.right, layer + );
}
} } public bool IsCousins(TreeNode root, int x, int y)
{
FrontTraversal(root);
var nodeX = L.Find(c => c.CurNode == x);
var nodeY = L.Find(c => c.CurNode == y);
if (nodeX != null && nodeY != null
&& nodeX.FatherNode != nodeY.FatherNode
&& nodeX.Layer == nodeY.Layer)
{
return true;
}
return false;
}
}

leetcode993的更多相关文章

  1. (二叉树 BFS) leetcode993. Cousins in Binary Tree

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

  2. [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 ...

  3. LeetCode993. 二叉树的堂兄弟节点

    题目 1 class Solution { 2 public: 3 TreeNode* r1;TreeNode* r2; 4 bool isCousins(TreeNode* root, int x, ...

随机推荐

  1. java线程实现的四种方式

    java多线程的实现可以通过以下四种方式 1.继承Thread类,重写run方法 2.实现Runnable接口,重写run方法 3.通过Callable和FutureTask创建线程 4.通过线程池创 ...

  2. Elasticsearch Internals: Networking Introduction An Overview of the Network Topology

    This article introduces the networking part of Elasticsearch. We look at the network topology of an ...

  3. bzoj5006: [THUWC2017 Bipartite]随机二分图

    某人在玩一个非常神奇的游戏.这个游戏中有一个左右各 nnn 个点的二分图,图中的边会按照一定的规律随机出现. 为了描述这些规律,某人将这些边分到若干个组中.每条边或者不属于任何组 (这样的边一定不会出 ...

  4. java浅析final关键字

    谈到final关键字,想必很多人都不陌生,在使用匿名内部类的时候可能会经常用到final关键字.另外,Java中的String类就是一个final类,那么今天我们就来了解final这个关键字的用法. ...

  5. P2799国王的魔镜

    链接 想了好久(蒟蒻的不能蒟蒻) 题解: #include<iostream>#include<cstdio>#include<cstring>#include&l ...

  6. flume-拦截器、channel选择器、sink组合sink处理器

    1. Flume Interceptors Flume有能力修改/删除流程中的events.这是在拦截器(interceptor)的帮助下完成的.拦截器(Interceptors)是实现org.apa ...

  7. spring4.0之九:websocket简单应用

    Spring 4.0的一个最大更新是增加了websocket的支持.websocket提供了一个在web应用中的高效.双向的通讯,需要考虑到客户端(浏览器)和服务器之间的高频和低延时消息交换.一般的应 ...

  8. 廖雪峰Java4反射与泛型-1反射-1Class类

    1.Class类与反射定义 Class类本身是一种数据类型(Type),class/interface的数据类型是Class,JVM为每个加载的class创建了唯一的Class实例. Class实例包 ...

  9. 廖雪峰Java1-4数组操作-4多维数组

    二维数组 二维数组就是元素为数组的数组 二维数组每个数组的长度不要求一样.比如 int[][] = { { 1, 2 }, { 3, 4, 5 }, { 6, 7, 8, 9 } } int[][] ...

  10. 刚刚研究了下ipv6,尝试配置内网VPS的IPv6地址

    刚刚研究了下ipv6,尝试配置内网VPS的IPv6地址是3台设备,分别是客户机Windows系统.核心交换机.PPPoE拨号的路由器 第一步:在PPPoE拨号的路由器上面查看ppp0拨号的地址 ifc ...