-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinary Tree Level Order Traversal II.py
50 lines (37 loc) · 1.6 KB
/
Binary Tree Level Order Traversal II.py
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
# https://leetcode.com/problems/binary-tree-level-order-traversal-ii/submissions/
# 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 levelOrderBottom(self, root: Optional[TreeNode]) -> List[List[int]]:
# If root is empty, return an empty list
if root is None:
return []
# If root doesn't have any kids, return a list of itself
if root.left is None and root.right is None:
return [[root.val]]
# Create 2 lists
finalList = list()
queue = [root]
# Iterate queue
while queue:
# Visit children
nodesOnLevel = list()
for idx in range(len(queue)):
# Get current node
currNode = queue.pop(0)
# If "currNode" isn't a leaf node, continue
if currNode is not None:
# Add currNode to finalList
nodesOnLevel.append(currNode.val)
# Add children to queue
queue.append(currNode.left)
queue.append(currNode.right)
# If any nodes existed on this level, add it to finalList
if nodesOnLevel:
finalList.append(nodesOnLevel)
# Reverse final list
return list(reversed(finalList))