tree.in
tree.out
A binary tree can be represented as a LISP expression in the form
(root left right), where root is the label of the
root node of the tree, and left and right are its
left and right sub-trees in the same representation. An empty tree is
represented as (), and a leaf node as
(root () ()).
Write a program that, for a given integer X and a binary tree whose nodes are labeled with integers, determines whether there exists a path from a leaf node of the tree to the root node where the sum of all labels on the path is X.
The file may contain several test cases, each on two lines. The first line of a test case contains an integer X (|X| ≤ 1,000,000) and the second line describes a binary tree in the form explained above. All the labels Ai satisfy |Ai| ≤ 1,000,000, all the node-to-root path sums Si satisfy |Si| ≤ 10,000,000, the size of the input file does not exceed 100,000 bytes, and there are no spaces in the file.
The file should contain one line for each test case. The line should contain the word YES, if there is at least one leaf-to-root path in the tree whose sum is X, and the word NO otherwise.
3 (2(1()())(0()())) 3 (2(2()())(0()())) 3 (2(1(1()())(1()()))()) 0 ()
YES NO NO NO