-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree-inorder-traversal.py
More file actions
26 lines (25 loc) · 900 Bytes
/
tree-inorder-traversal.py
File metadata and controls
26 lines (25 loc) · 900 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
# Given the root of a binary tree, return the inorder traversal of its nodes' values.
# 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: Optional[TreeNode]) -> List[int]:
# Case 1. Where there is no root node
if root == None:
return []
if root.left == None and root.right == None:
return [root.val]
inorder = []
not_visited = []
current = root
while current or not_visited:
while current:
not_visited.append(current)
current = current.left
current = not_visited.pop(-1)
inorder.append(current.val)
current = current.right
return inorder