[Explanations] symmetrical binary tree

Title Description

        The right to have a little bit of the root of the tree if the following conditions are met Xuan Xuan were called symmetric binary tree:

        1. binary tree;

        2. Insert the left and right subtrees of all tree nodes exchange, the same tree structure of the new and original tree corresponding positions and points of equal power.

        The numbers in the figure is the node weights, id nodes represent external node number.

Failed to load picture

        Now given a binary tree, I hope you find it a sub-tree, the tree is symmetrical sub binary tree, and a maximum number of nodes. Please tree output sub-tree nodes.

        Note: Only the roots of the trees are symmetrical binary tree. In this problem convention, to a sub-root node of T "subtree" means: binary tree node T and all of its successor nodes.

 

Input Format

        The first line, a positive integer n, the number of a given node of a tree, a predetermined node number 1-n, where 1 is the root node.

        Second row, n-positive integers, separated by a space, the i-th positive integer weights vi for node i.

        Next n lines of two positive integers li, ri, respectively, left and right child node number i. If the left / right child does not exist, -1 means. Separated by a space between the two numbers.

 

Output Format

        Total row contains an integer indicating the maximum tree given two symmetrical fork tree nodes.

 

SAMPLE INPUT

2

1 3

2 -1

-1 -1

 

Sample Output

1

 

answer

        Think of a symmetric palindromic, known string algorithms.

        manacher? will not. Then hash it.

        Over the whole tree traversal sequence, and then turn preorder (Right - Root - left) over the whole tree, to obtain two sequences traversal.

        Weights can be directly set hash. structure?

        In fact, we may correspond to the number of nodes of the subtree with a record hash for each node, so that it can be determined whether the same structure.

        Note: This insurance is entitled, like him to compare several hash, hash can be double or even three hash.

Reference program

 

Guess you like

Origin www.cnblogs.com/kcn999/p/11210898.html