-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path285. Inorder Successor in BST.py
60 lines (45 loc) · 1.64 KB
/
285. Inorder Successor in BST.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
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def inorderSuccessor(self, root: 'TreeNode', p: 'TreeNode') -> 'TreeNode':
stack=[] # just in case we need option 2
old=None
new=None
# implement option 1
curr=p
current=root
if not curr:
return None
if curr.right:
curr=curr.right
while curr.left:
curr=curr.left
return curr
# implement option 2
else:
while True:
# Reach the left most Node of the current Node
if current is not None:
# Place pointer to a tree node on the stack
# before traversing the node's left subtree
stack.append(current)
current = current.left
# BackTrack from the empty subtree and visit the Node
# at the top of the stack; however, if the stack is
# empty you are done
elif(stack):
current = stack.pop()
old=new
if old==p:
return current
new=current # Python 3 printing
# We have visited the node and its left
# subtree. Now, it's right subtree's turn
current = current.right
else:
break
#return f