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. SpringMVC拦截器配置

    1.首先在springmvc.xml中添加配置 <mvc:interceptors> <mvc:interceptor> <mvc:mapping path=" ...

  2. 基于folly的AtomicIntrusiveLinkedList无锁队列进行简单封装的多生产多消费模型

    1.基于folly的AtomicIntrusiveLinkedList略微修改的无锁队列代码: #ifndef FOLLY_REVISE_H #define FOLLY_REVISE_H namesp ...

  3. HDOJ 2008 数值统计

    #include<iostream> using namespace std; int main() { int n; ) { , y = , z = ; double t; ;i < ...

  4. 1118 Birds in Forest (25 分)

    1118 Birds in Forest (25 分) Some scientists took pictures of thousands of birds in a forest. Assume ...

  5. Activity的启动模式--总结

    3. Activity的任务栈Task以及启动模式与Intent的Flag详解? 2,Activity次级页面和主页间来回跳转,防止重复创建Activity实例 1, activity的启动模式: / ...

  6. Java JDK版本切换--绝逼好使

    转载: https://www.cnblogs.com/ll409546297/p/6593173.html 1.问题:同时装两个版本的jdk时出现的问题(本次是1.7和1.8的版本),因为eclip ...

  7. SCCM 2012 R2实战系列之一:SQL安装

    大家好,从今天开始跟大家一起分享自己学习SCCM 2012 R2的一些心得和具体的部署配置,希望能帮到大家.由于SCCM部署的步骤比较复杂,内容也比较多,所以我把SCCM整个部署过程分为以下三个章节: ...

  8. Linux mysql 5.7.23 主从复制(异步复制)

    docker容器主节点: 172.17.0.9  docker容器子节点: 172.17.0.10 异步复制: 首先确认主库和从库是否一致,最好都是刚刚初始化的干净的数据库 如果主库正在使用不能初始化 ...

  9. PDP context

    PDP context[edit] The packet data protocol (PDP; e.g., IP, X.25, FrameRelay) context is a data struc ...

  10. sqoop导出mysql数据进入hive错误

    看mr的运行显示:sqoop job可以获得的select max(xxx)结果,但是当mr开始时却显示大片错误,就是连接超时,和连接重置等问题, 最后去每个节点ping mysql的ip地址,发现 ...