Introduction to Algorithms Classic-fifth chapter 6-10 falling leaves

Source: Internet
Author: User
Tags array length

To a binary tree, each node has a horizontal position: The Left Dial hand node is on its left one unit, the right child node in the right
Side of a unit. Outputs the sum of weights for all nodes in each horizontal position from left to right. , left-to-right three
The position right and the 7,11,3 respectively, according to the recursive (first order) way input, uses 1 to represent the empty tree.

Sample Input

5 7-1 6-1-1 3-1-1

8 2 9-1-1 6 5-1-1 12-1-1 3 7-1-1-1-1


Sample Output

Case 1:7 11 3

Case 2:9 7 21 15

The title means to let us output the number of leaves from left to right.

To illustrate:

8 2 9-1-1 6 5-1-1 12-1-1 3 7-1-1-1-1

The binary tree structure is:

#include <cstring>#include<iostream>using namespacestd;Const intMAXN = $;intSUM[MAXN];//How to build a two-fork tree, and then in a series of complex processing, to tell the truth, this problem really did not think that there is such a clever technique, the direct use of the first-order traversal, first-to-end processing. Considering the meaning of the topic array length is 80, we start from subscript 40 execution, array[ 40]=8, and then executes the left subtree recursively, followed by the right sub-tree. The overall idea is that, when executing Zuozi, the incoming subscript-1, judging the value of the head node of the current left subtree, then continues to recursively call its left and right subtree ... //read in and build a tree, the horizontal position of the root is P cycle each treevoidBuildintp) {    intv; CIN>>v; if(v==-1)return;//read-1 Stopsum[p]+=v; //establish the left and right sub-treesBuild (P-1); Build (P+1);}BOOLinit () {intv; CIN>>v; if(v==-1)return false;//Otherwise, loop through each casememset (SUM,0,sizeof(sum)); intpos=maxn/2;//the horizontal position of the rootsum[pos]=v;//first sequence traversal, first left subtree, then right sub-treeBuild (POS-1); Build (POS+1);}intMain () {intKase =0;  while(init ()) {intp =0;  while(Sum[p] = =0) p++;//find the left-most leaf.//start the output. Because you want to avoid extra space at the end of the line, so a little more troublecout <<" Case"<< ++kase <<": \ n"<< sum[p++];  while(Sum[p]! =0) {cout<<" "<<Sum[p]; P++; } cout<<"\ n"; }  return 0;}

Introduction to Algorithms Classic-fifth chapter 6-10 falling leaves

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.