Question: The input begins with an integer t , which gives the number of test cases in the input. Each test case will consist of two
The input begins with an integer t, which gives the number of test cases in the input.
Each test case will consist of two lines. The first line will contain an integer n, which gives the number of nodes in the tree.
The next line will contain n, space-separated symbols representing the values from the pots-order traversal of the tree. These will be chosen from the following set of values: {&, |, ^, !, t, f}.
Sample input 2 6 t f | f ! ^ 8 f t t t ^ | & !
Sample Output:
false true
What I have so far, and is it possible to do it with an single stack?
With the sample test imput
the postordered tree should look like this...

What I have got so far.
import java.io.*; import java.util.*; import java.util.Scanner; import java.util.Stack;
public class Expression {
public static boolean Parse(String str) { char[] arr = str.toCharArray(); Stack else if (arr[i] == '|' || arr[i] == '!' || arr[i] == '^') { operator.push(arr[i]); if (operator.peek() == '!') bool.push(applyOp(operator.pop(), bool.pop(), false)); else bool.push(applyOp(operator.pop(), bool.pop(), bool.pop())); } } return bool.pop(); } public static boolean applyOp(char op, boolean b, boolean a) { switch (op) { case '|': return a | b; case '^': return a ^ b; case '!': return !a; } return false; } public static void main(String[] args){ Scanner in = new Scanner(System.in); int t = in.nextInt(); //t indicate the run cases int m = in.nextInt(); //contain nodes int cur=0; String a = in.nextLine(); for(int i =0; i }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
