Leetcode 94.中序遍历

94. 二叉树的中序遍历

方法

方法1:迭代

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None

class Solution(object):
def inorderTraversal(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
res=[]
if not root:
return res

result=self.inorderTraversal(root.left)
result.append(root.val)
result+=self.inorderTraversal(root.right)
return result
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None

class Solution(object):
def inorderTraversal(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
ans = []
def recursive(r):
if not r:
return
recursive(r.left)
ans.append(r.val)
recursive(r.right)
recursive(root)
return ans

方法2:迭代

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
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None

class Solution(object):
def inorderTraversal(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
if not root:
return []
res = []
point = root
stack = []
while point:
if point.left:
stack.append(point)
point = point.left
else:
res.append(point.val)
if point.right:
point = point.right
continue
else:
while stack and not point.right:
point = stack.pop()
res.append(point.val)
if not point.right:
return res
else:
point = point.right
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None

class Solution(object):
def inorderTraversal(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
stack,res=[],[]
while root or stack:
while root:
stack.append(root)
root=root.left
root=stack.pop()
res.append(root.val)
root=root.right
return res
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution:
# @param root, a tree node
# @return a list of integers
def inorderTraversal(self, root):
if root == None:
return []
result = []
visited = []
stack = [root]
while stack != []:
node = stack.pop()
if node in visited:
result.append(node.val)
continue
visited.append(node)
if node.right != None:
stack.append(node.right)
stack.append(node)
if node.left != None:
stack.append(node.left)
return result
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution:
# @param {TreeNode} root
# @return {integer[]}
def inorderTraversal(self, root):
result, stack = [], [(root, False)]

while stack:
cur, visited = stack.pop()
if cur:
if visited:
result.append(cur.val)
else:
stack.append((cur.right, False))
stack.append((cur, True))
stack.append((cur.left, False))

return result
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution:
def inorderTraversal(self, root):
stack = [ (False, root) ]
acc = []

while stack:
flag, val = stack.pop()
if val:
if not flag:
stack.append( (False, val.right) )
stack.append( (True, val) )
stack.append( (False, val.left) )
else:
acc.append( val.val )
return acc

参考资料