-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathleaf_list_v1.py
41 lines (31 loc) · 1.42 KB
/
leaf_list_v1.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
def leaf_list(root):
"""
Generates a list of leaf nodes' values in a binary tree using a Depth-First Search (DFS) approach.
Args:
root: The root node of the binary tree.
Returns:
A list of values of leaf nodes in the binary tree, or an empty list if the tree is empty.
"""
if root is None: # Handle empty tree case
return []
stack = [root] # Initialize stack with root node
leaf_list = [] # List to store leaf node values
while stack:
current = stack.pop() # Pop the top node from the stack
# Check if the current node is a leaf node (has no children)
if current.right is None and current.left is None:
leaf_list.append(current.val) # Append leaf node value to the list
# Otherwise, add children to the stack for further exploration (right child first, then left child)
else:
if current.right is not None:
stack.append(current.right)
if current.left is not None:
stack.append(current.left)
return leaf_list # Return the list of leaf node values
"""
Time Complexity: O(n), where n is the number of nodes
The algorithm visits each node in the tree once using DFS. In the worst case, all nodes are visited,
leading to linear time complexity.
Space Complexity: O(n) in the worst case
The stack can hold up to a maximum of n/2 nodes in a skewed tree, resulting in linear space complexity.
"""