Consider the following binary tree structure and code to find its height. What will be the printed height?
class Node: def __init__(self, val): self.val = val self.left = None self.right = None def height(root): if root is None: return 0 left_height = height(root.left) right_height = height(root.right) return 1 + max(left_height, right_height) # Constructing the tree root = Node(10) root.left = Node(5) root.right = Node(15) root.left.left = Node(3) root.left.right = Node(7) root.right.right = Node(18) print(height(root))
Height counts the number of nodes along the longest path from root to leaf.
The longest path is from root (10) to leaf (3 or 7 or 18) which has 3 nodes, so height is 3.
Given the same height function, what is the height after adding a new node as the left child of node 3?
class Node: def __init__(self, val): self.val = val self.left = None self.right = None def height(root): if root is None: return 0 left_height = height(root.left) right_height = height(root.right) return 1 + max(left_height, right_height) root = Node(10) root.left = Node(5) root.right = Node(15) root.left.left = Node(3) root.left.right = Node(7) root.right.right = Node(18) root.left.left.left = Node(1) # New node added here print(height(root))
Adding a node deeper on the left subtree increases height by 1.
The longest path is now 10 -> 5 -> 3 -> 1, which has 4 nodes, so height is 4.
What error will this code raise when trying to compute the height of the tree?
class Node: def __init__(self, val): self.val = val self.left = None self.right = None def height(root): if root is None: return 0 left_height = height(root.left) right_height = height(root.right) return 1 + max(left_height, right_height) root = Node(10) root.left = Node(5) root.right = Node(15) root.left.left = Node(3) root.left.right = Node(7) root.right.right = Node(18) root.left.left.left = 1 # Incorrect assignment print(height(root))
Check the type of the left child of node 3.
Assigning an integer instead of a Node causes height(root.left.left.left) to call height(1), which fails when accessing 1.left.
Consider a binary tree where every node has only a right child, forming a chain of 7 nodes. What is the height of this tree?
Height counts nodes along the longest path from root to leaf.
Since the tree is a chain of 7 nodes, height is 7.
Starting with an empty tree, nodes are inserted in this order: 10, 5, 15, 3, 7, 18, 1, 0. Using the height function, what is the height after all insertions?
class Node: def __init__(self, val): self.val = val self.left = None self.right = None def insert(root, val): if root is None: return Node(val) if val < root.val: root.left = insert(root.left, val) else: root.right = insert(root.right, val) return root def height(root): if root is None: return 0 return 1 + max(height(root.left), height(root.right)) root = None for v in [10, 5, 15, 3, 7, 18, 1, 0]: root = insert(root, v) print(height(root))
Trace the longest path from root to leaf after all insertions.
The longest path is 10 -> 5 -> 3 -> 1 -> 0, which has 5 nodes, so height is 5.
