forked from algorhythms/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
095 Binary Tree Inorder Traversal.py
100 lines (85 loc) · 2.42 KB
/
095 Binary Tree Inorder Traversal.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
93
94
95
96
97
98
99
100
"""
Given a binary tree, return the inorder traversal of its nodes' values.
For example:
Given binary tree {1,#,2,3},
1
\
2
/
3
return [1,3,2].
Note: Recursive solution is trivial, could you do it iteratively?
confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ.
"""
__author__ = 'Danyang'
class TreeNode(object):
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution(object):
def inorderTraversal(self, root):
"""
Morris Traversal
"""
ret = []
cur = root
while cur:
if not cur.left:
ret.append(cur.val)
cur = cur.right
else:
pre = cur.left
while pre.right and pre.right != cur:
pre = pre.right
if not pre.right:
pre.right = cur
cur = cur.left
else:
pre.right = None
ret.append(cur.val)
cur = cur.right
return ret
def inorderTraversal_memory(self, root):
"""
:type root: TreeNode
:param root:
:return: a list of integers
"""
lst = []
self.inorderTraverse_itr(root, lst)
return lst
def inorderTraverse(self, root, lst):
"""
In order traverse
"""
if not root:
return
self.inorderTraverse(root.left, lst)
lst.append(root.val)
self.inorderTraverse(root.right, lst)
def inorderTraverse_itr(self, root, lst):
"""
iterative version
leftmost first in the lst
double loop
reference: http://fisherlei.blogspot.sg/2013/01/leetcode-binary-tree-inorder-traversal.html
:type root: TreeNode
:param root:
:param lst:
:return:
"""
if not root:
return
cur = root
stk = []
while stk or cur:
while cur:
stk.append(cur)
cur = cur.left
cur = stk.pop() # left_most
lst.append(cur.val)
cur = cur.right
# if cur.right: # should go to next iteration
# cur = cur.right
# stk.append(cur)