-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathBinaryTreeInorderTraversal.py
More file actions
37 lines (36 loc) · 1.03 KB
/
BinaryTreeInorderTraversal.py
File metadata and controls
37 lines (36 loc) · 1.03 KB
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
class Solution(object):
def inorderTraversal(self, root):
st = [(root,0)]
p = []
if not root: return p
while st:
h,c = st.pop()
if c==1 or (not h.left and not h.right): p.append(h.val)
else:
if h.right: st.append((h.right,0))
st.append((h,1))
if h.left: st.append((h.left,0))
return p
'''
# Morris traversal
class Solution(object):
def inorderTraversal(self, root):
p = []
cur = root
while cur:
if not cur.left:
p.append(cur.val)
cur= cur.right
else:
prev = cur.left
while prev.right and prev.right!=cur:
prev = prev.right
if not prev.right:
prev.right = cur
cur = cur.left
else:
prev.right = None
p.append(cur.val)
cur = cur.right
return p
'''