Traverse both the given trees in a PreOrder style.
At every step, check if the current node exists for both the trees. If one of these children happens to be null, we return the child of the other tree to be added as a child subtree to the calling parent node in the first tree.
We can add the values in the current nodes of both the trees and update the value in the current node of the first tree to reflect this sum obtained.
Then we call the with the left children and then with the right children of the current nodes of the two trees.
At the end, the first tree will represent the required resultant merged binary tree.
for (int i = 0; 2 * i < n; i++) { Pe[i] = a[2 * i]; Po[i] = a[2 * i + 1]; }
// Divide 分治 // 递归求 ye = Pe(xi), yo = Po(xi) vector<Complex> ye = FFT(Pe, invert); vector<Complex> yo = FFT(Po, invert);
// Combine vector<Complex> y(n);
// Root of Units double ang = 2 * PI / n * (invert ? -1 : 1); Complex wn(cos(ang), sin(ang)); // wn为第1个n次复根, Complex w(1, 0); // w为第零0个n次复根, 即为 1
for (int i = 0; i < n / 2; i++) { y[i] = ye[i] + w * yo[i]; // 求出P(xi) y[i + n / 2] = ye[i] - w * yo[i]; // 由单位复根的性质可知第k个根与第k + n/2个根互为相反数 w = w * wn; // w * wn 得到下一个复根 }