wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Stack and Queue Quiz

Total questions: 168

Worksheet time: 1hrs 29mins

Name
Class
Date
1.

Which of the following data structure works on Last In First Out (LIFO) principle?

a)

Queue

b)

Stack

c)

Array

d)

Linked List

2.

Which operation inserts an element into a stack?

a)

Add

b)

Enqueue

c)

Push

d)

Insert

3.

What is the operation called that removes an element from the top of the stack?

a)

Delete

b)

Remove

c)

Dequeue

d)

Pop

4.

In a stack, where is the new element added?

a)

Bottom

b)

Top

c)

Middle

d)

Anywhere

5.

What will be the output after performing operations: push(10), push(20), pop()?

a)

10

b)

20

c)

30

d)

Error

6.

Which data structure is used for undo operations in text editors?

a)

Queue

b)

Array

c)

Stack

d)

Tree

7.

What happens when you pop from an empty stack?

a)

Returns 0

b)

Inserts null

c)

Stack Overflow

d)

Stack Underflow

8.

Which Java class can be used to implement a stack?

a)

java.util.LinkedList

b)

java.util.Stack

c)

java.util.HashMap

d)

java.util.Array

9.

What is the time complexity of push and pop operations in a stack?

a)

O(n)

b)

O(log n)

c)

O(1)

d)

O(n log n)

10.

How many stacks are needed to implement a queue?

a)

1

b)

2

c)

3

d)

None

11.

Which of the following is NOT a valid stack operation?

a)

Push

b)

Pop

c)

Peek

d)

Enqueue

12.

What does peek() do in a stack?

a)

Removes the top element

b)

Shows the top element

c)

Deletes the stack

d)

Adds a new element

13.

Which of the following application uses stack?

a)

Printer Spooler

b)

Job Scheduling

c)

Function Call Management

d)

Buffer Management

14.

Stack is a type of:

a)

Linear Data Structure

b)

Non-linear

c)

Graph

d)

Tree

15.

Which traversal of tree uses stack?

a)

BFS

b)

Inorder

c)

DFS

d)

Level Order

16.

What is the initial value of the top pointer in an empty stack (array implementation)?

a)

-1

b)

0

c)

NULL

d)

1

17.

Which of the following is a disadvantage of stack?

a)

Fast insertion

b)

Limited access

c)

Easy memory management

d)

Dynamic size

18.

In expression evaluation, stack is used for:

a)

Infix evaluation

b)

Prefix evaluation

c)

Postfix evaluation

d)

Bracket removal

19.

Which data structure is used for backtracking?

a)

Queue

b)

Graph

c)

Stack

d)

Heap

20.

Stack overflow occurs when:

a)

Stack underflows

b)

Stack becomes NULL

c)

Stack gets empty

d)

Stack exceeds its size limit

21.

What is the return value of pop() when stack is empty (in most languages)?

a)

-1

b)

Exception or Error

c)

NULL

d)

0

22.

Which of the following is a valid sequence of stack operations?

a)

push(1), push(2), pop(), push(3)

b)

pop(), push(1), push(2)

c)

push(1), enqueue(2)

d)

insert(1), delete(2)

23.

Which of these uses a call stack internally?

a)

Loops

b)

Pointers

c)

Recursive function

d)

Arrays

24.

What happens if too many recursive calls are made without a base case?

a)

Null error

b)

Infinite loop

c)

Stack Underflow

d)

Stack Overflow

25.

What does stack.peek() return after push(7), push(9), push(11)?

a)

7

b)

9

c)

0

d)

11

26.

Which expression format is easiest to evaluate using a stack?

a)

Infix

b)

Postfix

c)

Prefix

d)

Bracketed

27.

What is the main use of stack in system programming?

a)

Store loop counters

b)

Manage heap memory

c)

Manage function calls and local variables

d)

Display output

28.

What is the maximum number of elements that can be stored in a stack of size n?

a)

n+1

b)

n-1

c)

n

d)

Infinite

29.

What is a stack frame?

a)

Stack node

b)

Storage structure for a single function call

c)

Block in heap

d)

Loop block

30.

In array implementation of stack, how is stack underflow checked?

a)

If top == -1

b)

If top == size

c)

If top == NULL

d)

If stack[0] == NULL

31.

Which of the following postfix expressions is valid?

a)

23+5

b)

23+5*

c)

235+

d)

23*+5

32.

What is the result of evaluating the postfix expression 5 6 2 + *?

a)

60

b)

40

c)

70

d)

32

33.

What will be the output of the following Java stack operations? Stack s = new Stack<>(); s.push(1); s.push(2); s.pop(); s.push(3); System.out.println(s.peek());

a)

1

b)

2

c)

3

d)

Stack is empty

34.

Which data structure is best suited for balancing symbols in an expression (like { [ ( ) ] })?

a)

Queue

b)

Stack

c)

Heap

d)

Tree

35.

What is the infix expression for the postfix: A B + C *?

a)

(A + B) * C

b)

A + (B * C)

c)

A + B * C

d)

A * B + C

36.

What is the postfix of the infix: (A + B) * (C - D)?

a)

AB+CD-*

b)

ABC+D-*

c)

A+B*C-D

d)

AB+*CD-

37.

Which of the following will NOT cause StackOverflow?

a)

Infinite loop

b)

Deep recursion

c)

Too many push operations

d)

No pop operation

38.

Which of the following will NOT cause StackOverflow?

a)

Infinite loop

b)

Deep recursion

c)

Too many push operations

d)

No pop operation

39.

Which of the following is used to implement function calls internally in programming languages?

a)

Stack

b)

Queue

c)

Array

d)

Graph

40.

Which is true about peek() operation in stack?

a)

It removes the top element

b)

It throws an exception if the stack is full

c)

It returns the top without removing

d)

It adds an element

41.

What will be the result of this prefix expression: + 3 * 5 6?

a)

33

b)

30

c)

21

d)

18

42.

Which of the following postfix expressions corresponds to the infix A + B * C?

a)

AB+C*

b)

ABC*+

c)

A+BC*

d)

ABC+*

43.

A stack implemented using linked list requires memory for:

a)

Data only

b)

Pointer only

c)

Data and pointer

d)

Just index

44.

If a stack has n elements, what is the time complexity of searching an element?

a)

O(1)

b)

O(log n)

c)

O(n)

d)

O(n log n)

45.

Which traversal method uses a stack?

a)

BFS

b)

Preorder using recursion

c)

Level order

d)

All of the above

46.

In which of the following applications is the stack data structure most extensively used?

a)

File systems

b)

Page replacement algorithms

c)

Expression evaluation

d)

Round Robin scheduling

47.

Which of the following operations can result in an underflow in a stack?

a)

push()

b)

pop()

c)

peek()

d)

insert()

48.

What will be the output of the following operations on an empty stack? Stack s = new Stack<>(); s.push(10); s.push(20); s.pop(); s.push(30); System.out.println(s.peek());

a)

10

b)

20

c)

30

d)

Stack is empty

49.

What is the time complexity of push and pop operations in a stack implemented using an array or linked list?

a)

O(log n)

b)

O(n)

c)

O(1)

d)

O(n log n)

50.

What is the postfix form of the expression: A + B * (C - D)?

a)

AB+CD-*

b)

ABCD-+

c)

AB+C-D

d)

A+BCD*-

51.

Which method is used to return the top element without removing it from the stack in Java?

a)

top()

b)

peek()

c)

pop()

d)

fetch()

52.

What happens if you perform a pop() operation on an empty stack in Java?

a)

Returns -1

b)

Returns null

c)

Throws EmptyStackException

d)

Returns 0

53.

Which of the following is NOT a valid use case for stacks?

a)

Undo operations in editors

b)

Function call management

c)

Graph traversal

d)

CPU scheduling

54.

In stack terminology, which end is considered the top?

a)

The first inserted element

b)

The element at index 0

c)

The most recently inserted element

d)

The element with the highest priority

55.

Which data structure is used to reverse a word or a sentence in programming?

a)

Queue

b)

Stack

c)

List

d)

Graph

56.

Which of the following statements about stacks is FALSE?

a)

Stacks use LIFO principle

b)

Stacks can be used for DFS

c)

Stacks can grow dynamically

d)

Stacks allow direct access to elements

57.

How many stacks are needed to implement a queue using stacks?

a)

1

b)

2

c)

3

d)

0

58.

What will be the result of the postfix expression 7 3 2 * -?

a)

1

b)

-1

c)

7

d)

1

59.

Which Java class provides built-in stack functionality?

a)

Queue

b)

LinkedList

c)

Stack

d)

List

60.

Which of the following is true about stack memory in recursion?

a)

Each recursive call uses the same memory

b)

Recursive calls share the same variables

c)

Stack memory increases with recursion depth

d)

Stack is not involved in recursion

61.

Which of the following postfix expressions is valid and correctly evaluates to 14?

a)

5 3 + 2 *

b)

6 2 * 2 +

c)

4 4 + 2

d)

7 2 3 *

62.

Which of the following is First In First Out (FIFO)?

a)

Stack

b)

Queue

c)

Tree

d)

Graph

63.

Which operation inserts an element in a queue?

a)

Push

b)

Enqueue

c)

Insert

d)

Pop

64.

What operation removes an element from the queue?

a)

Pop

b)

Delete

c)

Dequeue

d)

Peek

65.

Queue insertion happens at the:

a)

Front

b)

Rear

c)

Middle

d)

Top

66.

Queue deletion happens at the:

a)

Rear

b)

Top

c)

Front

d)

End

67.

What is the condition of Queue Overflow?

a)

When front = rear

b)

When queue is full

c)

When queue is empty

d)

When no dequeue occurs

68.

What is the time complexity of enqueue and dequeue?

a)

O(log n)

b)

O(n)

c)

O(n log n)

d)

O(1)

69.

Circular Queue overcomes the problem of:

a)

Insertion

b)

Deletion

c)

Memory Waste

d)

Overflow

70.

What is the default size of Java’s LinkedList queue?

a)

Infinite

b)

10

c)

100

d)

0

71.

Which interface in Java is implemented for a queue?

a)

Stack

b)

Queue

c)

Deque

d)

Map

72.

Priority Queue is:

a)

FIFO

b)

LIFO

c)

Based on priority

d)

Sorted Queue

73.

In which scenario is queue preferred?

a)

Backtracking

b)

Call Stack

c)

Resource Scheduling

d)

Tree Traversal

74.

What is the drawback of a simple queue?

a)

It uses a lot of memory

b)

Insertion is not possible in middle

c)

Space at front is wasted

d)

Deletion is complex

75.

What is the output of the queue after: enqueue(10), enqueue(20), dequeue()?

a)

[10]

b)

[20]

c)

[10, 20]

d)

[]

76.

Which of the following queue types allows insertion and deletion from both ends?

a)

Simple Queue

b)

Priority Queue

c)

Deque

d)

Circular Queue

77.

Which of the following is not a valid queue type?

a)

Simple Queue

b)

Double-Ended Queue

c)

Priority Queue

d)

Binary Queue

78.

In Java, which class is commonly used to implement a queue?

a)

Stack

b)

ArrayList

c)

LinkedList

d)

TreeMap

79.

In Java, which class is commonly used to implement a queue?

a)

Stack

b)

ArrayList

c)

LinkedList

d)

TreeMap

80.

What is the time complexity of accessing the front element in a queue?

a)

O(n)

b)

O(log n)

c)

O(1)

d)

O(n log n)

81.

Which method is used in Java to insert an element in a queue?

a)

add()

b)

insert()

c)

offerLast()

d)

enqueue()

82.

Which method removes the front element of a queue in Java?

a)

pop()

b)

remove()

c)

pollLast()

d)

get()

83.

What happens if you call remove() on an empty Java Queue?

a)

Returns null

b)

Throws NullPointerException

c)

Throws NoSuchElementException

d)

Does nothing

84.

What does the offer() method return if the insertion fails?

a)

true

b)

false

c)

null

d)

throws exception

85.

Which method retrieves but does not remove the head of the queue?

a)

remove()

b)

element()

c)

peek()

d)

poll()

86.

In a circular queue, what condition represents overflow?

a)

rear == size

b)

front == rear

c)

(rear + 1) % size == front

d)

rear == -1

87.

How many pointers are used to manage a circular queue?

a)

1

b)

2

c)

3

d)

4

88.

Which of the following is not an operation on a queue?

a)

Enqueue

b)

Dequeue

c)

Traverse

d)

Peek

89.

What is the result of this operation: enqueue(5), enqueue(8), dequeue(), enqueue(10)?

a)

[5, 8, 10]

b)

[8, 10]

c)

[5, 10]

d)

[10]

90.

What is the purpose of a Deque?

a)

Only insert at front

b)

Insert at front, delete at rear

c)

Insert and delete from both ends

d)

Acts like a stack

91.

Which interface in Java supports Deque implementation?

a)

Queue

b)

List

c)

Deque

d)

Set

92.

What happens when you dequeue from an empty queue?

a)

It wraps to front

b)

It causes overflow

c)

It causes underflow

d)

It returns front

93.

What is the main advantage of a circular queue over a linear queue?

a)

Easy insertion

b)

Better memory utilization

c)

Faster operations

d)

Simpler implementation

94.

In a circular queue, the condition for full queue is:

a)

front == rear

b)

front == -1

c)

(rear + 1) % size == front

d)

rear == size

95.

What is the output of the queue after: Queue q = new LinkedList<>(); q.add(1); q.add(2); q.remove(); q.add(3); System.out.println(q);

a)

[1, 2, 3]

b)

[2, 3]

c)

[1, 3]

d)

[3, 2]

96.

Which Java interface is suitable for queue operations?

a)

List

b)

Collection

c)

Queue

d)

Set

97.

What is the role of the offer() method in queues?

a)

Adds to the front

b)

Adds to the rear

c)

Removes from front

d)

Removes from rear

98.

In which situation do we prefer Deque over queue?

a)

When insertion at rear is required

b)

When deletion from front is required

c)

When both ends require insertion/deletion

d)

Never

99.

Priority Queue differs from normal queue in terms of:

a)

Insertion logic

b)

Deletion logic

c)

Both A and B

d)

Memory usage

100.

What will happen if poll() is called on an empty queue?

a)

Exception

b)

Returns -1

c)

Returns null

d)

Inserts 0

101.

What is the correct time complexity of inserting into a priority queue (min-heap)?

a)

O(1)

b)

O(log n)

c)

O(n)

d)

O(n^2)

102.

Which of the following data structures can be used to implement BFS in a graph?

a)

Stack

b)

Queue

c)

Tree

d)

Set

103.

Which of these can be used for implementing undo-redo feature in a browser?

a)

Queue only

b)

Two stacks

c)

Circular Queue

d)

Priority Queue

104.

Which is not a valid operation on a queue?

a)

Dequeue

b)

Peek

c)

Insert front

d)

Offer

105.

Which class is used in Java for a double-ended queue?

a)

Deque

b)

Stack

c)

PriorityQueue

d)

ArrayList

106.

Which queue data structure uses heap internally in Java?

a)

Deque

b)

ArrayDeque

c)

PriorityQueue

d)

BlockingQueue

107.

Queue is the best data structure for:

a)

Recursion

b)

Parsing

c)

Scheduling processes

d)

Sorting

108.

Which of the following is the correct postfix form of the expression: A + B * C?

a)

ABC+*

b)

A + BC*

c)

ABC*+

d)

+A*BC

109.

What is the prefix form of the expression: (A - B) / (C + D)?

a)

-AB/+CD

b)

/-AB+CD

c)

/-AB+CD

d)

/AB+CD-

110.

Convert the infix expression A * (B + C) / D to prefix:

a)

*A/+BCD

b)

/ * A + B C D

c)

/ * A + B C D

d)

/ * A + B C D

111.

The postfix expression for: (A + B) * (C - D) is:

a)

AB+CD-*

b)

AB+*CD-

c)

A+B*C-D

d)

ABCD+-*

112.

Evaluate the postfix expression: 2 3 1 * + 9 -

a)

-4

b)

4

c)

2

d)

0

113.

Which data structure is most suitable for converting infix to postfix?

a)

Array

b)

Queue

c)

Stack

d)

LinkedList

114.

What is the postfix notation for A - B + C?

a)

ABC+-

b)

AB-C+

c)

A-BC+

d)

AB+C-

115.

Prefix form of the expression A + B * (C ^ D - E)?

a)

+A*B^-CDE

b)

+A*B^CDE-

c)

+A*B^-CDE

d)

+A*B^-CDE

116.

Which of the following statements is true?

a)

Postfix uses parentheses to indicate precedence.

b)

Prefix is easier to evaluate using a queue.

c)

Postfix expressions are evaluated from left to right using a stack.

d)

Infix is easier for computers to evaluate.

117.

Evaluate the postfix: 5 1 2 + 4 * + 3 -

a)

10

b)

14

c)

15

d)

16

118.

In a valid postfix expression, operators appear:

a)

Before their operands

b)

After their operands

c)

Between operands

d)

Cannot be determined

119.

Which conversion is correct for: (A + B) * (C - D)

a)

Infix → Prefix: * +AB -CD

b)

Infix → Postfix: AB+CD-*

c)

Infix → Prefix: *+AB-CD

d)

All of the above

120.

What is the prefix of A * B + C / D?

a)

*AB+/CD

b)

+*AB/CD

c)

+AB*/CD

d)

+*ABCD/

121.

Which postfix expression is equivalent to: A + B - C * D?

a)

AB+CD*-

b)

AB+CD*-

c)

ABCD*+-

d)

AB+*CD-

122.

Evaluate: 8 2 / 3 - 2 *

a)

8

b)

6

c)

4

d)

2

123.

The order of evaluation in prefix expressions is:

a)

Right to left

b)

Left to right

c)

Inside to outside

d)

Depends on operators

124.

The infix expression for the postfix: AB+C* is:

a)

A + (B * C)

b)

(A + B) * C

c)

A + B * C

d)

A * (B + C)

125.

Which traversal of an expression tree gives the postfix expression?

a)

Inorder

b)

Postorder

c)

Preorder

d)

Level-order

126.

Which one is a valid prefix expression?

a)

AB+

b)

+AB

c)

A+B

d)

AB+*

127.

What is the postfix of: (A + B * C - D) ^

4 lines
128.

Which one is a valid prefix expression?

a)

AB+

b)

+AB

c)

A+B

d)

AB+*

129.

What is the postfix of: (A + B * C - D) ^ E ^ F

a)

ABC*+D−EF^^

b)

ABC*+DEF^^-

c)

AB*C+D-EF^^

d)

ABC*+D-EF^^

130.

What is the result of evaluating the postfix expression: 6 2 3 + - 3 8 2 / + *?

a)

10

b)

20

c)

30

d)

40

131.

What is the result of evaluating the postfix expression: 5 6 2 + * 12 4 / -

a)

30

b)

32

c)

28

d)

24

132.

What is the result of evaluating the postfix expression: 7 8 + 3 2 + /

a)

2

b)

3

c)

5

d)

7

133.

What is the result of evaluating the postfix expression: 10 2 8 * + 3 -

a)

23

b)

27

c)

19

d)

20

134.

What is the result of evaluating the postfix expression: 4 5 7 2 + - *

a)

12

b)

10

c)

-8

d)

8

135.

What is the result of evaluating the postfix expression: 3 4 + 2 5 * +

a)

17

b)

27

c)

35

d)

21

136.

What is the result of evaluating the postfix expression: 12 3 / 2 * 4 +

a)

8

b)

12

c)

10

d)

6

137.

What is the result of evaluating the postfix expression: 15 7 1 1 + - / 3 * 2 1 1 + + -

a)

3

b)

5

c)

1

d)

2

138.

What is the result of evaluating the postfix expression: 2 3 1 * + 9 -

a)

-4

b)

4

c)

0

d)

5

139.

What is the result of evaluating the postfix expression: 4 2 + 3 5 1 - * +

a)

24

b)

20

c)

16

d)

22

140.

What is the result of evaluating the postfix expression: 5 1 2 + 4 * + 3 -

a)

14

b)

18

c)

22

d)

16

141.

In postfix evaluation, which data structure is used?

a)

Queue

b)

Stack

c)

Heap

d)

Tree

142.

Postfix expression 2 3 1 * + 9 - evaluates to:

a)

2

b)

5

c)

-4

d)

4

143.

What will be the result of this postfix expression: 4 5 7 2 + - *

a)

4

b)

-16

c)

20

d)

12

144.

Which operator is evaluated first in the expression: 4 5 + 2 3 ^ *?

a)

+

b)

*

c)

^

d)

Depends on precedence

145.

Fill in the blank to evaluate a postfix expression using a stack: for (char ch : expr) { if (Character.isDigit(ch)) { stack.push(ch - '0'); } else { int b = stack.pop(); int a = stack.pop(); int result = // (a)   stack.push(result); } }

146.

What is the output of the following Python code? expr = "23*54*+9-" stack = [] for ch in expr: if ch.isdigit(): stack.append(int(ch)) else: b = stack.pop() a = stack.pop() if ch == '*': stack.append(a * b) elif ch == '+': stack.append(a + b) elif ch == '-': stack.append(a - b) print(stack[-1])

a)

17

b)

25

c)

23

d)

18

147.

What will be the output of the following Java code? String expr = "231*+9-"; Stack s = new Stack<>(); for (char ch : expr.toCharArray()) { if (Character.isDigit(ch)) { s.push(ch - '0'); } else { int b = s.pop(); int a = s.pop(); switch (ch) { case '+': s.push(a + b); break; case '-': s.push(a - b); break; case '*': s.push(a * b); break; } } } System.out.println(s.peek());

a)

5

b)

4

c)

2

d)

-4

148.

Which of the following can be the output of evaluating this postfix: 8 2 / 3 - 2 *?

a)

5

b)

6

c)

8

d)

4

149.

Identify the correct postfix evaluation function (Java style):

a)

if (isOperator(ch)) { int a = pop(); int b = pop(); push(a + b); }

b)

if (isOperator(ch)) { int b = pop(); int a = pop(); push(a + b); }

c)

if (isOperator(ch)) { int b = pop(); int a = pop(); push(b + a); }

d)

if (isOperator(ch)) { int b = pop(); int a = pop(); push(a - b); }

150.

Which of the following is not a tree traversal method?

a)

Inorder

b)

Preorder

c)

Reverseorder

d)

Postorder

151.

In which traversal is the root node visited first?

a)

Inorder

b)

Preorder

c)

Postorder

d)

Level-order

152.

In a postorder traversal, the root is visited:

a)

First

b)

After the left subtree

c)

After the right subtree

d)

Last

153.

What is the inorder traversal of a binary tree with root 1, left child 2, and right child 3?

a)

1 2 3

b)

2 1 3

c)

3 2 1

d)

2 3 1

154.

Level-order traversal is also known as:

a)

Depth First Traversal

b)

Breadth First Traversal

c)

Postorder Traversal

d)

Spiral Traversal

155.

Which traversal is useful to get the prefix expression of an expression tree?

a)

Inorder

b)

Postorder

c)

Preorder

d)

Level-order

156.

What data structure is used for level-order traversal?

a)

Stack

b)

Queue

c)

Tree

d)

Heap

157.

Which traversal visits left subtree, root, then right subtree?

a)

Preorder

b)

Postorder

c)

Inorder

d)

Level-order

158.

Which traversal is best suited for deleting a tree?

a)

Inorder

b)

Postorder

c)

Preorder

d)

Level-order

159.

Preorder traversal of a binary tree is:

a)

Root → Left → Right

b)

Left → Root → Right

c)

Left → Right → Root

d)

Right → Left → Root

160.

Given a binary tree, if its inorder and preorder traversal are known, which traversal can be constructed uniquely?

a)

Postorder

b)

Level-order

c)

Preorder

d)

Inorder

161.

If inorder traversal of a binary tree is [D, B, E, A, F, C], what is the preorder traversal?

a)

A, B, D, E, C, F

b)

D, B, A, E, C, F

c)

A, D, B, E, F, C

d)

A, B, E, D, C, F

162.

Which traversal method is used to copy a binary tree?

a)

Inorder

b)

Postorder

c)

Preorder

d)

Level-order

163.

In which order do nodes get pushed into a stack for iterative inorder traversal?

a)

Right → Root → Left

b)

Left → Root → Right

c)

Root → Left → Right

d)

Left → Right → Root

164.

Which traversal gives nodes in non-decreasing order for a BST?

a)

Preorder

b)

Postorder

c)

Inorder

d)

Level-order

165.

You are given a binary tree with the preorder traversal [A, B, D, E, C, F]. What is the root?

a)

A

b)

B

c)

D

d)

F

166.

For expression trees, which traversal gives the postfix expression?

a)

Inorder

b)

Preorder

c)

Postorder

d)

Level-order

167.

Which traversal is most appropriate for evaluating an expression tree?

a)

Inorder

b)

Postorder

c)

Preorder

d)

Level-order

168.

In which traversal can you print all the leaf nodes in a left-to-right manner?

4 lines