-
Notifications
You must be signed in to change notification settings - Fork 7
/
Program.cs
36 lines (35 loc) · 795 Bytes
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
using System;
using System.Collections.Generic;
namespace LowestCommonAncestorOfABinaryTreeIII
{
public class Node
{
public int val;
public Node left;
public Node right;
public Node parent;
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
static Node LowestCommonAncestor(Node p, Node q)
{
var set = new HashSet<Node>();
while (p != null)
{
set.Add(p);
p = p.parent;
}
while (q != null)
{
if (set.Contains(q))
return q;
q = q.parent;
}
return null;
}
}
}