-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathBSTSuccessor_Step1.cs
60 lines (56 loc) · 1.35 KB
/
BSTSuccessor_Step1.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BSTSuccessor
{
/*
*
* Given a node n in a binary search tree, explain and code the most efficient way to
* find the successor of n.
Analyze the runtime complexity of your solution.
*/
public class Node
{
public Node left;
public Node right;
public Node parent;
public int val;
}
class BSTSuccessor_Step1
{
static void Main(string[] args)
{
}
/*
* step 1:
* work on simple test case
*
* 4
* 3 5
*
* There are bugs in the thinking of successor
*
* For node 4, the successor is 5
* For node 5, the successor is null,
* For node 3, the successor is 4
*/
public static Node getSuccessor(Node nd)
{
if (nd == null)
return null;
if (nd.right == null)
{
if (nd.parent != null && nd.parent.left == nd)
{
return nd.parent;
}
else
return null;
}
else
return nd.right;
}
}
}