Small monkey whereabouts (binary tree)

Source: Internet
Author: User

Small monkey Fall time limit:MS | Memory limit:65535 KB Difficulty:3
Describe

There is a binary tree with a maximum depth of D, and all leaves have the same depth. All nodes are numbered 1,2,3,,2 from left to right from top to bottom minus 1 for D-Times. Put a little monkey at the junction 1 and it will run down. Each inner node has a switch, the initial all closed, when a small monkey ran to a switch, its state will change, when reaching an internal node, if the switch is closed, the small monkey go left, or go to the right, until the leaf node.

Some little monkeys began to run down from the Junction 1, where did the last little monkey go?

Input
enter the depth of the binary tree leaves D, and the number of small monkeys I, assuming I do not exceed the number of leaves of the whole tree, d<=20. End With 0 0
Output
output the number of the leaf where I have a small monkey.
Sample input
4 23 40 0
Sample output
127
Traversal by layer is the assignment of data from 1 to n
1#include <iostream>2 using namespacestd;3typedefstructnode4 {5     BOOLvalue;6     intdata;7Node *lchild,*Rchild;8}binode,*Bitree;9 ints=1;TenBitree a[10000000]; OneBitree Createtree (Bitree root,intdepth) A { -     if(depth==1) -     { theroot=NewBinode; -Root->lchild=root->rchild=NULL; -Root->value=0; -Root->data=s; +s++; -         returnRoot; +     } A     Else at     { -root=NewBinode; -Root->value=0; -Root->data=s; -s++; -Root->lchild=createtree (root->lchild,depth-1); inRoot->rchild=createtree (root->rchild,depth-1); -     } to     returnRoot; + } - voidTraverse (bitree root) the { *     if(Root) $     {Panax Notoginsengcout<<root->data<<" "; -Traverse (root->lchild); theTraverse (root->rchild); +     } A } the intMonkey (bitree root) + { -     if(Root) $     {     $         if(root->lchild==null&&root->rchild==NULL) -         { -             if(root->value==1) theRoot->value=0; -             ElseWuyiRoot->value=1; the             returnRoot->data; -         } Wu         if(root->value==1) -         { AboutRoot->value=0; $             returnMonkey (root->rchild); -         } -         Else -         { ARoot->value=1; +             returnMonkey (root->lchild); the         } -     } $ } the voidtraversequeue (bitree root) the { the      the     intI=1; -     intFont=0, rear=0; in     if(Root) the     { thea[rear]=Root; Aboutrear++; the     } the      while(rear!=font) the     { +Bitree p=A[font]; -font++; thep->data=i;i++;Bayi         if(p->lchild) the         { theA[rear]=p->Lchild; -rear++; -         } the         if(p->rchild) the         { theA[rear]=p->Rchild; therear++; -         } the     } the } the intMain ()94 { the     intd,n,i; the      while(cin>>d>>N) the     {98s=1; About         if(d==0&&n==0) -             return 0;101Bitree root=NULL;102root=Createtree (root,d);103         //Traverse (root);104         //cout<<endl; the Traversequeue (root);106          for(i=0; i<n-1; i++)107 Monkey (root);108Cout<<monkey (Root) <<Endl;109     } the}

Small monkey whereabouts (binary tree)

Related Article

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.