-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInorderTraversal.py
More file actions
36 lines (32 loc) · 959 Bytes
/
InorderTraversal.py
File metadata and controls
36 lines (32 loc) · 959 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
27
28
29
30
31
32
33
34
35
36
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
# from collections import deque
class Solution:
def inorderTraversal(self, root: TreeNode) -> List[int]:
result = []
stack = []
# if root:
# result = self.printinorder(root, result)
# print(root)
while root or stack:
# print(root)
if root:
stack.append(root)
root = root.left
continue
root = stack.pop()
result.append(root.val)
root = root.right
return result
# def printinorder(self, root, res):
# if root:
# if root.left:
# self.printinorder(root.left, res)
# res.append(root.val)
# if root.right:
# self.printinorder(root.right, res)
# return res