Title: Give you a tree in the root sequence and the posterior root sequence traversal, the path from the root to the leaves of the number and the smallest.
Analysis: DS, tree, recursive. The direct recursive solution is updated with global variables.
Description: If the path is the same as the same, the output of the lowest leaf value.
#include <algorithm> #include <iostream> #include <cstdlib> #include <cstring> #include < Cstdio> #include <cmath>using namespace std;int p[10001];int i[10001];int min,leaf;void dfs (int il, int ir, int pl , int PR, int value) {int f = il,l,r;while (i[f]! = P[PR]) + + f;if (f > IL) dfs (IL, f-1, pl, pl+f-il-1, VALUE+P[PR]); if (f < IR) DFS (f+1, IR, Pl+f-il, Pr-1, VALUE+P[PR]); if (pl = = pr) {if (min > P[pl]+value) {min = P[pl]+value; Leaf = P[pl];} else if (Min = = P[pl]+value && leaf > p[pl]) Leaf = P[PL];}} int main () {char ch;while (~scanf ("%d", &i[0]) {int count = 1;while (GetChar ()! = ' \ n ') scanf ("%d", &i[count + +]); fo R (int i = 0; i < count; + + i) scanf ("%d%c", &p[i],&ch); Min = 100000001; leaf = -1;dfs (0, count-1, 0, count-1, 0);p rintf ("%d\n", leaf);} return 0;}
UVa 548-tree