LeetCode Problem: 102. Binary Tree Level Order Traversal
https://leetcode.com/problems/binary-tree-level-order-traversal/description/
Problem Description:
Given the root of a binary tree, return the level order traversal of its nodes’ values. (i.e., from left to right, level by level).
Example 1:
Input: root = [3, 9, 20, null, null, 15, 7]
Output: [[3], [9, 20], [15, 7]]
Example 2:
Input: root = [1]
Output: [[1]]
Example 3:
Input: root = []
Output: []
Original Code (BFS Approach):
from collections import deque
class Solution:
def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:
if not root:
return [] # If the tree is empty, return an empty list
res = [] # Initialize the result list
queue = deque([root]) # Initialize the queue with the root
while queue: # While there are nodes to process
level_size = len(queue) # Number of nodes at the current level
level_nodes = [] # List to hold nodes at the current level
for _ in range(level_size): # Process all nodes at the current level
node = queue.popleft() # Dequeue the front node
level_nodes.append(node.val) # Add the node's value to the current level's list
# Enqueue the left and right children, if they exist
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)
res.append(level_nodes) # Add the current level's values to the result
return res # Return the final result
Line-by-Line Explanation and Example Execution:
-
from collections import deque
- Imports
deque
for efficient queue operations.
- Imports
-
class Solution:
- Defines the
Solution
class.
- Defines the
-
def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:
- Defines the main function
levelOrder
, which takes the root of a binary tree and returns a list of lists containing the level order traversal.
- Defines the main function
-
if not root:
- Checks if the tree is empty. If so, returns an empty list.
-
res = []
- Initializes an empty list to store the result of the level order traversal.
-
queue = deque([root])
- Initializes a queue with the root node to keep track of nodes to be processed.
-
while queue:
- Continues processing as long as there are nodes in the queue.
-
level_size = len(queue)
- Stores the number of nodes at the current level.
-
level_nodes = []
- Initializes a list to hold the values of nodes at the current level.
-
for _ in range(level_size):
- Iterates through all nodes at the current level.
-
node = queue.popleft()
- Dequeues the front node from the queue.
-
level_nodes.append(node.val)
- Adds the value of the current node to the list of nodes at the current level.
-
if node.left:
- Checks if the left child exists. If so, enqueues it.
-
if node.right:
- Checks if the right child exists. If so, enqueues it.
-
res.append(level_nodes)
- Adds the current level’s values to the result list.
-
return res
- Returns the final result after processing all levels.
Example Execution:
Assuming we have the following binary tree:
3
/ \
9 20
/ \
15 7
Execution steps:
-
Initialization:
root = 3
res = []
,queue = deque([3])
-
First Level:
level_size = 1
,level_nodes = []
- Dequeue
3
, add tolevel_nodes
→level_nodes = [3]
- Enqueue
9
and20
. - Add
level_nodes
tores
→res = [[3]]
.
-
Second Level:
level_size = 2
,level_nodes = []
- Dequeue
9
, add tolevel_nodes
→level_nodes = [9]
- Dequeue
20
, add tolevel_nodes
→level_nodes = [9, 20]
- Enqueue
15
and7
. - Add
level_nodes
tores
→res = [[3], [9, 20]]
.
-
Third Level:
level_size = 2
,level_nodes = []
- Dequeue
15
, add tolevel_nodes
→level_nodes = [15]
- Dequeue
7
, add tolevel_nodes
→level_nodes = [15, 7]
- Add
level_nodes
tores
→res = [[3], [9, 20], [15, 7]]
.
-
Final Result:
- Return
res = [[3], [9, 20], [15, 7]]
.
- Return
Time Complexity Analysis:
- Time Complexity: O(n)
- Where n is the number of nodes in the tree. Each node is processed once.
Space Complexity Analysis:
- Space Complexity: O(n)
- In the worst case, the queue can hold up to n nodes (in case of a complete binary tree).
Tips and Warnings:
-
Edge Cases:
- Consider cases where the tree is empty or consists of a single node.
-
Understanding BFS:
- Ensure understanding of breadth-first search and how it explores levels in the tree.
-
Level Order Traversal:
- Familiarity with how to implement level order traversal will help in solving similar problems.
Summary
- BFS Method: Effectively performs level order traversal of a binary tree, with a time complexity of O(n) and a space complexity of O(n).
- Clear and Understandable: The code is straightforward and easy to understand, suitable for handling this type of problem.
Application Tips
-
Choose Appropriate Methods:
- Select the method best suited to specific problem requirements to ensure efficiency and readability.
-
Handle Edge Cases:
- Always consider edge cases in algorithm design to avoid potential errors.
-
Optimize Space Usage:
- When handling large datasets, consider using space-efficient algorithms.
Leave a Reply