Question link:
Http://uva.onlinejudge.org/index.php? Option = com_onlinejudge & Itemid = 8 & category = 104 & page = show_problem & problem = 640
Question type: data structure, binary tree
Question:
In the autumn of every year, the leaves in the north are accompanied by brilliant colors. The leaves are falling under the tree with the wind, and the ground will soon accumulate a lot.
If the same thing happens in a binary tree and the nodes on the tree are slowly falling down, what kind of scene should we look like?
A binary tree is also a tree.
For a binary tree, each node is regarded as a branch, and the value on the node is the number of leaves on the branch. For each node, the distance between the left son and the right son is a unit.
For example, root node 5 and 6 are on the same vertical line, and 7 and 5 are separated by one unit.
Then, when autumn came, the leaves of the binary tree fell, and the weather was good and there was no wind, so the leaves of the binary tree fell vertically. Finally, the leaves of the nodes in the same vertical line all fall into the same heap.
. Let's find the number of leaves on each pile.
Solution:
The question shows a string of numbers, which are traversed in the forward order.-1 indicates that the node is empty.
First, we can recursively construct this binary tree, and then perform a deep search to calculate the number of infected stacks.
During the calculation, I used the trick to open an array of 1000 results, 500 Coordinate Position of the stub node, and then recursion, go to the left son, coordinate-1, go to the right son, coordinates + 1.
This question can also be computed directly on the recursion of the build. The following are two versions of code.
Sample input:
5 7 -1 6 -1 -1 3 -1 -18 2 9 -1 -1 6 5 -1 -1 12 -1-1 3 7 -1 -1 -1-1
Sample output:
Case 1:7 11 3Case 2:9 7 21 15
Version 1: Build version
#include<iostream>#include<cstdio>#include<cstring>using namespace std;const int END = -2147483645;int arr[1000], aSize, result[1000];class Node{public: int data; Node *father; Node *left; Node *right;};Node node[1000];int indexNode, pos;inline Node* BuildRoot(int n){ indexNode = 1; node[0].data = n; node[0].father = NULL; node[0].left = node[0].right = NULL; return &node[0];}inline Node* NewNode(){ node[indexNode].left = NULL; node[indexNode].right = NULL; return &node[indexNode++];}Node* build(Node *root){ if(pos>=aSize) return NULL; if(arr[pos]==-1) { return NULL; } root = NewNode(); root->data = arr[pos]; ++pos; root->left = build(root->left); ++pos; root->right = build(root->right); return root;}void preOrder(Node *root){ if(root){ printf("%d ", root->data); preOrder(root->left); preOrder(root->right); }}void dfs(Node *root, int index){ if(root){ result[index] += root->data; // printf("%d: %d, ",root->data, index); dfs(root->left, index-1); dfs(root->right, index+1); }}void Solve(){ Node *root=NULL; pos=0; indexNode = 0; root = build(root); memset(result, 0, sizeof(result)); dfs(root, 500); int i=0; while(result[i]==0) ++i; printf("%d", result[i++]); for( ; result[i]!=0; ++i) printf(" %d", result[i]); printf("\n\n"); }int main(){ freopen("input.txt","r",stdin); int cas=1; while(~scanf("%d", &arr[0]) && arr[0]!=-1){ aSize = 1; int cnt1=1, cnt2=0; while(getchar()){ scanf("%d", &arr[aSize++]); if(arr[aSize-1]==-1) ++cnt2; else ++cnt1; if(cnt1+1==cnt2) break; } printf("Case %d:\n", cas++); Solve(); } return 0;}
Version 2: no new version
#include<iostream>#include<cstdio>#include<cstring>using namespace std;int arr[1000], aSize, result[1000];int indexNode, pos;void build(int index){ if(pos>=aSize) return ; if(arr[pos]==-1) { return ; } result[index] += arr[pos]; ++pos; build(index-1); ++pos; build(index+1);}void Solve(){ pos=0; indexNode = 0; memset(result, 0, sizeof(result)); build(500); int i=0; while(result[i]==0) ++i; printf("%d", result[i++]); for( ; result[i]!=0; ++i) printf(" %d", result[i]); printf("\n\n"); }int main(){ // freopen("input.txt","r",stdin); int cas=1; while(~scanf("%d", &arr[0]) && arr[0]!=-1){ aSize = 1; int cnt1=1, cnt2=0; while(getchar()){ scanf("%d", &arr[aSize++]); if(arr[aSize-1]==-1) ++cnt2; else ++cnt1; if(cnt1+1==cnt2) break; } printf("Case %d:\n", cas++); Solve(); } return 0;}
-- The meaning of life is to give it meaning.
OriginalHttp://blog.csdn.net/shuangde800,
D_double