Q. What is the output of an in-order traversal of the binary tree with root 1, left child 2, and right child 3?
A.
1, 2, 3
B.
2, 1, 3
C.
3, 1, 2
D.
1, 3, 2
Show solution
Solution
In in-order traversal, we visit the left child first, then the parent, and finally the right child. Thus, the output is 2, 1, 3.
Correct Answer:
B
— 2, 1, 3
Learn More →
Q. What is the output of the following in-order traversal on the binary tree: 1, 2, 3, 4, 5?
A.
1, 2, 3, 4, 5
B.
5, 4, 3, 2, 1
C.
2, 1, 4, 3, 5
D.
1, 3, 2, 5, 4
Show solution
Solution
In-order traversal visits the left subtree, then the node, and finally the right subtree, resulting in the sorted order of the nodes.
Correct Answer:
A
— 1, 2, 3, 4, 5
Learn More →
Q. What is the primary purpose of a queue in the context of binary tree traversal?
A.
To store nodes for post-order traversal
B.
To store nodes for in-order traversal
C.
To store nodes for level-order traversal
D.
To store nodes for pre-order traversal
Show solution
Solution
A queue is primarily used for level-order traversal of a binary tree, allowing nodes to be processed in the order they are encountered.
Correct Answer:
C
— To store nodes for level-order traversal
Learn More →
Q. What is the primary purpose of a stack in the context of binary tree traversal?
A.
To store tree nodes
B.
To reverse the order of traversal
C.
To keep track of visited nodes
D.
To implement recursion
Show solution
Solution
A stack is used to implement recursion iteratively, allowing for depth-first traversal of the binary tree.
Correct Answer:
D
— To implement recursion
Learn More →
Q. What is the result of a post-order traversal on a binary tree with nodes 1, 2, and 3, where 1 is the root, 2 is the left child, and 3 is the right child?
A.
1, 2, 3
B.
2, 3, 1
C.
3, 2, 1
D.
1, 3, 2
Show solution
Solution
In post-order traversal, we visit the left child, then the right child, and finally the parent. Thus, the output is 2, 3, 1.
Correct Answer:
B
— 2, 3, 1
Learn More →
Q. What is the space complexity of a recursive implementation of a binary tree traversal?
A.
O(1)
B.
O(n)
C.
O(log n)
D.
O(n log n)
Show solution
Solution
The space complexity is O(n) due to the recursion stack in the worst case, where n is the number of nodes in the tree.
Correct Answer:
B
— O(n)
Learn More →
Q. Which of the following algorithms can be used to find the height of a binary tree?
A.
Depth-first search
B.
Breadth-first search
C.
Both depth-first and breadth-first search
D.
None of the above
Show solution
Solution
Both depth-first search (DFS) and breadth-first search (BFS) can be used to find the height of a binary tree.
Correct Answer:
C
— Both depth-first and breadth-first search
Learn More →
Q. Which of the following is a valid way to represent a binary tree node in C++?
A.
struct Node { int data; Node* left; Node* right; };
B.
class Node { int data; Node* left; Node* right; };
C.
struct Node { int data; Node left; Node right; };
D.
class Node { int data; Node left; Node right; };
Show solution
Solution
The correct representation of a binary tree node in C++ is using a struct with pointers to the left and right children.
Correct Answer:
A
— struct Node { int data; Node* left; Node* right; };
Learn More →
Q. Which traversal method would you use to create a mirror image of a binary tree?
A.
In-order
B.
Pre-order
C.
Post-order
D.
Level-order
Show solution
Solution
Pre-order traversal is used to create a mirror image of a binary tree by swapping the left and right children of each node.
Correct Answer:
B
— Pre-order
Learn More →
Q. Which traversal method would you use to get the nodes of a binary tree in post-order?
A.
Visit left, visit right, visit node
B.
Visit node, visit left, visit right
C.
Visit right, visit left, visit node
D.
Visit left, visit node, visit right
Show solution
Solution
Post-order traversal follows the order: visit left subtree, visit right subtree, and then visit the node.
Correct Answer:
A
— Visit left, visit right, visit node
Learn More →
Q. Which traversal method would you use to get the nodes of a binary tree in reverse level order?
A.
Pre-order
B.
In-order
C.
Post-order
D.
Level-order
Show solution
Solution
Level-order traversal visits nodes level by level, and to get them in reverse order, you can use a stack to reverse the order after the traversal.
Correct Answer:
D
— Level-order
Learn More →
Showing 1 to 11 of 11 (1 Pages)