-
Notifications
You must be signed in to change notification settings - Fork 7
/
Program.cs
62 lines (55 loc) · 1.29 KB
/
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using System;
using System.Collections.Generic;
namespace InorderSuccessorInBSTII
{
public class Node
{
public int val;
public Node left;
public Node right;
public Node parent;
public Node()
{
}
public Node(int num)
{
val = num;
}
}
class Program
{
static void Main(string[] args)
{
var a = new Node(5);
var b = new Node(3);
var c = new Node(6);
var d = new Node(2);
var e = new Node(4);
var f = new Node(1);
a.left = b;
a.right = c;
b.left = d;
b.right = e;
b.parent = a;
c.parent = a;
d.left = f;
d.parent = b;
e.parent = b;
f.parent = d;
Console.WriteLine(InorderSuccessor(e));
}
static Node InorderSuccessor(Node x)
{
if (x.right != null)
{
x = x.right;
while (x.left != null)
x = x.left;
return x;
}
while (x.parent != null && x == x.parent.right)
x = x.parent;
return x.parent;
}
}
}