Question 1
EasyWhat is the maximum number of children of a node in a binary tree?
- A.
Popular companies
Prepare company by company
Hiring pattern, syllabus, past questions and mocks for each company.
All companiesPractice by subject
Data Structures and Algorithms
What is the maximum number of children of a node in a binary tree?
3
Unlimited
2
1
Answer: C
Each node has at most a left child and a right child.
What is a leaf node?
A node with one child
A node with no children
The root node
A node with two children
Answer: B
Leaves are at the ends of the tree's branches.
In which traversal is the root visited first?
None of them
Inorder
Postorder
Preorder
Answer: D
Preorder: root, left, right. Inorder: left, root, right. Postorder: left, right, root.
What is the maximum number of nodes in a binary tree of height h (with a single root node having height 0)?
h²
2^(h+1) − 1
2h + 1
2^h
Answer: B
Each level doubles: 1 + 2 + 4 + … + 2^h = 2^(h+1) − 1.
Which traversal visits nodes level by level?
Level-order (breadth-first) traversal
Postorder traversal
Inorder traversal
Preorder traversal
Answer: A
It uses a queue to visit all nodes at depth 0, then depth 1, and so on.
What is the output of this code?
class Node:
def __init__(self, v, l=None, r=None):
self.v, self.l, self.r = v, l, r
root = Node(1, Node(2, Node(4), Node(5)), Node(3))
def pre(n):
return [] if not n else [n.v] + pre(n.l) + pre(n.r)
print(*pre(root))
4 5 2 3 1
4 2 5 1 3
Answer: D
Preorder visits the root, then the left subtree, then the right: 1, 2, 4, 5, 3.
How many edges does a tree with n nodes have?
2n
n − 1
n
n + 1
Answer: B
Every node except the root has exactly one edge to its parent.
What is the root of a tree?
The last node added
Any leaf node
The topmost node, which has no parent
The node with the largest value
Answer: C
Every other node can be reached from the root.
What is the depth of a node in a tree?
The number of leaves below it
The value stored in it
The number of edges on the path from the root to that node
The number of its children
Answer: C
Count the edges from the root down to the node; the root itself has depth 0.
In a tree, what are siblings?
Nodes that share the same parent
Nodes at the same depth in different subtrees only
Leaves only
A node and its parent
Answer: A
For example, the left and right children of a node are siblings.
1 2 3 4 5
1 2 4 5 3