Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a class for a binary tree node.
DSA Python
class Node: def __init__(self, val): self.val = val self.left = None self.right = [1]
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 or val instead of None for child nodes.
Assigning self to right child by mistake.
✗ Incorrect
The left and right children of a node are initially set to None because they don't point to any node yet.
2fill in blank
mediumComplete the code to add the root node to the tree.
DSA Python
class BinaryTree: def __init__(self): self.root = [1]
Attempts:
3 left
💡 Hint
Common Mistakes
Creating a Node without value for root.
Assigning 0 or self to root.
✗ Incorrect
The root starts as None because the tree is empty initially.
3fill in blank
hardFix the error in the function to get the left side view using level order traversal.
DSA Python
def left_side_view(root): if not root: return [] queue = [root] result = [] while queue: level_length = len(queue) for i in range(level_length): node = queue.pop(0) if i == [1]: result.append(node.val) if node.left: queue.append(node.left) if node.right: queue.append(node.right) return result
Attempts:
3 left
💡 Hint
Common Mistakes
Using level_length - 1 which gives rightmost nodes.
Using i or 1 which do not select the leftmost node.
✗ Incorrect
The left side view shows the first node at each level, which is when i == 0.
4fill in blank
hardFill both blanks to create a recursive helper function for left side view.
DSA Python
def left_view_helper(node, level, [1]): if not node: return if level == len([2]): result.append(node.val) left_view_helper(node.left, level + 1, result) left_view_helper(node.right, level + 1, result)
Attempts:
3 left
💡 Hint
Common Mistakes
Using queue or visited which are not used in recursion here.
Using level instead of result for length check.
✗ Incorrect
The helper function uses 'result' to store left side nodes and checks its length to decide when to add a node.
5fill in blank
hardFill all three blanks to complete the recursive left side view function.
DSA Python
def left_side_view_recursive(root): result = [] def helper(node, level, [1]): if not node: return if level == len([2]): [3].append(node.val) helper(node.left, level + 1, result) helper(node.right, level + 1, result) helper(root, 0, result) return result
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names for the list.
Using queue which is not used in recursion.
✗ Incorrect
The 'result' list is passed and used in all blanks to collect left side nodes during recursion.
