leetcode993
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的更多相关文章
- (二叉树 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 ...
- [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 ...
- LeetCode993. 二叉树的堂兄弟节点
题目 1 class Solution { 2 public: 3 TreeNode* r1;TreeNode* r2; 4 bool isCousins(TreeNode* root, int x, ...
随机推荐
- ubuntu更新提示/boot空间不足
1. 查看当前使用的内核版本 uname -a 2.在终端下察看已经安装的旧的内核: ctrl+alt+t——>进入终端——>输入命令: dpkg --get-selections|gre ...
- django获取表单数据
django获取单表数据的三种方式 v1 = models.Business.objects.all() # v1是QuerySet的列表 ,内部元素都是对象 v2 = models.Business ...
- ORM多表操作之创建关联表及添加表记录
创建关联表 关于表关系的几个结论 (1)一旦确立表关系是一对多:建立一对多关系----在多对应的表中创建关联字段. (2)一旦确立表关系是多对多:建立多对多关系----创建第三张关系表----id和两 ...
- re模块小结
一.引子: 文件err.txt中有如下内容: 要求提取出所有的电话号码来. 方法一:文件操作法: f = open('eer.txt','r',encoding='utf-8') l = [] for ...
- [UE4]子控件Child Widget顶层容器选择
如果父级容器是Canvas,则可以直接设置尺寸.放到其他widget的时候也会保持设定好的尺寸(而不管父容器是什么类型).
- [电脑知识点]Excel取消受保护视图
- 将本地代码提交至gitHub
1.注册github账号 2.本地安装git 3.打开需要提交代码的目录 4.右击git bash here 5. $ git init 6 $ ssh-keygen -t rsa -C " ...
- 2-自己动手写HashMap
public class Entry { // 键 private Object key; // 值 private Object value; //构造器 public Entry(Object k ...
- 备用DNS域名服务器
DNS:1.34.151.129,域名:www#eliuliang#com, 个人用解析地址,请勿使用.
- Java - 35 Java 实例
Java 实例 本章节我们将为大家介绍 Java 常用的实例,通过实例学习我们可以更快的掌握 Java 的应用. Java 环境设置实例 Java 实例 – 如何编译一个Java 文件? Java 实 ...