package javaapplication1; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.util.ArrayList; import java.util.Scanner; class Tree { Tree left; Tree right; int valRoot; int valL; int valR; int sum; int index; boolean empty = false; boolean leaf = false; public Tree(Scanner sc, int parentSum){ sc.useDelimiter("\\s+"); sc.next(); if (sc.hasNextInt()){ valRoot = sc.nextInt(); sum = parentSum + valRoot; } else empty = true; if(!empty){ left = new Tree(sc, sum); right = new Tree(sc, sum); if(left.empty && right.empty) leaf = true; checkBranch(); } else { valR = 0; valL = 0; } String g = sc.next(); //) } public void checkBranch(){ if(leaf == true){ if (JavaApplication1.inSum == sum) JavaApplication1.isOK = true; } } } public class JavaApplication1 { /** * @param args the command line arguments */ static int inSum; static boolean isOK; public static void main(String[] args) throws FileNotFoundException { Scanner sc = null; String p; StringBuilder line = new StringBuilder(); try { sc = new Scanner(new BufferedReader(new FileReader("test"))); sc.useDelimiter("\\s+"); inSum = sc.nextInt(); while(sc.hasNext()){ line.append(sc.next()); } p = new String(line.toString()); p = p.replaceAll("\\(", " ( "); p = p.replaceAll("\\)", " ) "); } finally { if(sc != null) sc.close(); } sc = new Scanner(p); sc.useDelimiter("\\s+"); while (sc.hasNext("\\(")){ Tree t = new Tree(sc, 0); if (isOK) System.out.println("yes"); else System.out.println("no"); isOK = false; if(sc.hasNextInt()) inSum = sc.nextInt(); } } }