Which of the following data structures can be used for parentheses matching?

Options

  • A. priority queue
  • B. queue
  • C. n-ary tree
  • D. stack

Correct Answer (Detailed Explanation is Below)

D. stack

Detailed Explanation

Parentheses matching requires:

  • Checking opening brackets: ({[

  • Matching them with corresponding closing brackets: )}]

A Stack follows LIFO (Last In, First Out) principle:

  1. Push opening bracket onto stack

  2. When closing bracket appears → pop from stack

  3. Check if it matches

  4. If stack is empty at end → parentheses are balanced

OOps! You are currently offline.