-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinaryInoder(Leetcode94).py
More file actions
50 lines (39 loc) · 972 Bytes
/
Copy pathbinaryInoder(Leetcode94).py
File metadata and controls
50 lines (39 loc) · 972 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
"""
Given a binary tree, return the inorder traversal of its nodes' values.
Example:
Input: [1,null,2,3]
1
\
2
/
3
Output: [1,3,2]
"""
# Definition for a binary tree node.
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
class Solution:
def inorderTraversal(self, root):
stack, res = [], []
current = root
while True:
if current:
stack.append(current)
current = current.left
if len(stack) == 0:
return res
if current is None:
node = stack.pop()
res.append(node.val)
current = node.right
def main():
root = TreeNode(1)
root.right = TreeNode(2)
root.right.left = TreeNode(3)
solve = Solution()
print(solve.inorderTraversal(root))
if __name__ == '__main__':
main()