forked from ScarlettCC/AlgorithmByPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetNext.py
More file actions
26 lines (26 loc) · 746 Bytes
/
GetNext.py
File metadata and controls
26 lines (26 loc) · 746 Bytes
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
# -*- coding:utf-8 -*-
class TreeLinkNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
self.next = None
class Solution:
def GetNext(self, pNode):
# write code here
pnext = None
if pNode is None:
return pnext
if pNode.right is not None:
nextnode = pNode.right
while nextnode and nextnode.left:
nextnode = nextnode.left
pnext = nextnode
elif pNode.next:
pcur = pNode
pparent = pNode.next
while pparent and pparent.left!=pcur:
pcur = pparent
pparent = pcur.next
pnext = pparent
return pnext