All problems

Problem 925

Equal Tree Partition

Medium · 20 pts · Tree, Depth-First Search

Problem

Decide whether you can remove exactly one edge of the binary tree so that the two resulting trees have equal sums of values. Removing the edge above a node splits off that node's subtree, so you need a subtree (not the whole tree) whose sum is exactly half of the total. Compute every subtree sum with one post-order traversal, but do not count the root's own sum, because it is not separated by any edge.

Input

The first line contains N, the number of tokens. The second line contains N tokens describing the tree in level order (breadth-first, left to right), where 'null' marks a missing child, as on LeetCode.

Output

Print YES if such an edge exists, otherwise NO.

Constraints

1 <= number of nodes <= 10000; -10^5 <= value <= 10^5

Example input

7
5 10 10 null null 2 3

Example output

YES

Explanation

In 5 10 10 null null 2 3, the total is 30 and the subtree under the right child 10 has sum 10 + 2 + 3 = 15. Cutting the edge above it gives two trees of sum 15, so the answer is YES.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Height of Binary Tree After Subtree Removal Queries Next: Construct String from Binary Tree