-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path144_binaryTreePreorderTraversal.py
More file actions
39 lines (37 loc) · 1.28 KB
/
Copy path144_binaryTreePreorderTraversal.py
File metadata and controls
39 lines (37 loc) · 1.28 KB
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
#144. Binary Tree Preorder Traversal
# 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 preorderTraversal(self, root: TreeNode) -> List[int]:
# l = []
# if(root):
# l.append(root.val)
# l+=self.preorderTraversal(root.left)
# l+=self.preorderTraversal(root.right)
# return l
nodeStack = []
nodeStack.append(root)
# Pop all items one by one. Do following for every popped item
# a) print it
# b) push its right child
# c) push its left child
# Note that right child is pushed first so that left
# is processed first */
if(root is None):
return []
l = []
while(len(nodeStack) > 0):
# Pop the top item from stack and print it
node = nodeStack.pop()
l.append(node.val)
# Push right and left children of the popped node
# to stack
if node.right is not None:
nodeStack.append(node.right)
if node.left is not None:
nodeStack.append(node.left)
return l