leetcode993

时间:2023-08-20 14:39:08
 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;
}
}

相关文章