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. 测试教程网.unittest教程.8. 断言异常

    From: http://www.testclass.net/pyunit/assert_raise/ 背景 我们有时候需要断言一些方法会抛出异常,这些异常需要符合我们的预期. 代码 新建test_e ...

  2. 【ThreadLocal】使用ThreadLocal实现线程安全

    非线程安全 public class UnSafeThreadLocalDemo { private int count = 0; public static void main(String[] a ...

  3. python 数组 变成 字典的方法

    1.现在有两个列表,list1 = ['key1','key2','key3']和list2 = ['1','2','3'],把他们转为这样的字典:{'key1':'1','key2':'2','ke ...

  4. 廖雪峰Java5集合-6Stack-1使用Stack

    1.栈的定义 栈Stack是一种后进先出(LIFO: Last In First Out)的数据结构,可以看作一端封闭的容器,先进去的元素永远在底部,最后出来. 栈有2个重要的方法: push(E e ...

  5. 初识MapReduce

    MapReduce是Google的一项重要技术,它首先是一个编程模型,用以进行大数据量的计算.对于大数据量的计算,通常采用的处理手法就是并行计算.但对许多开发者来说,自己完完全全实现一个并行计算程序难 ...

  6. .net core 认证

    http://www.cnblogs.com/RainingNight/p/cookie-authentication-in-asp-net-core.html

  7. Servlet(API)生命周期

    一.最上层接口Servlet 查看Servlet接口源码: 有5个方法 访问过程(默认): 1.进行Servlet类加载 当Tomcat容器启动后,服务器寻找应用部署的描述文件(web.xml),从部 ...

  8. 基于pyQt5开发的股价显示器(原创)

    #/usr/bin/env python # -*- coding: utf-8 -*- ''' @author="livermorium116" 为了绕开公司内网而开发的 股票实 ...

  9. EventBus的使用;消息传递之EventBus;

    EventBus传递消息(数据)和广播有点像,对广播传递数据有兴趣的可以看一下:Android数据传递,使用广播BroadcastReceiver: 1.添加build.gradle implemen ...

  10. solr 打分和排序机制(转载)

    以下来自solr in action. 包含: 词项频次.查询词项出现在当前查询文档中的次数. 反向文档频次.查询词项出现在所有文档总的次数. 此项权重. 标准化因子: 字段规范: 文档权重. 字段权 ...