-
Notifications
You must be signed in to change notification settings - Fork 2
/
InorderSuccessorInBST.py
92 lines (71 loc) · 1.55 KB
/
InorderSuccessorInBST.py
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# Definition for a binary tree node.
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
# Iterative
class Solution1:
def inorderSuccessor(self, root: 'TreeNode', p: 'TreeNode') -> 'TreeNode':
if not root:
return None
succ = None
while root:
if p.val < root.val:
succ = root
root = root.left
else:
root = root.right
return succ
# Recursive
class Solution2:
def inorderSuccessor(self, root: 'TreeNode', p: 'TreeNode') -> 'TreeNode':
if not root:
return None
if p.val < root.val:
return self.inorderSuccessor(root.left, p) or root
return self.inorderSuccessor(root.right, p)
########
# Test #
########
# 2
# / \
# 1 3
root = TreeNode(2)
node1 = TreeNode(1)
node2 = TreeNode(3)
root.left = node1
root.right = node2
p = TreeNode(1)
print(Solution2().inorderSuccessor(root, p).val)
# expected: 2
# 2
# \
# 3
root = TreeNode(2)
node1 = TreeNode(3)
root.right = node1
p = TreeNode(2)
print(Solution2().inorderSuccessor(root, p).val)
# expected: 2
# 5
# / \
# 3 6
# / \
# 2 4
# /
# 1
root = TreeNode(5)
node1 = TreeNode(3)
node2 = TreeNode(6)
node3 = TreeNode(2)
node4 = TreeNode(4)
node5 = TreeNode(1)
root.left = node1
root.right = node2
node1.left = node3
node1.right = node4
node3.left = node5
p = TreeNode(6)
print(Solution2().inorderSuccessor(root, p))
# expected: None