Search for Binary Trees and Binary Trees

Source: Internet
Author: User

Search for Binary Trees and Binary Trees
[Problem description] It is known that a binary tree is stored in the structure of an adjacent table. In the middle order, find the node with the value of x in the binary tree and point out the number of nodes. For example, the data format of binary tree data files is as follows: 7155 2 312 4 510 0 029 0 015 6 78 0 023 0 0 •'

1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 using namespace std; 5 struct node 6 {7 int parent; 8 int date; 9 int lchild; 10 int rchild; 11 int wz; 12} a [101]; 13 int n; 14 int k; // the value to be searched is 15 int now = 0; 16 void zx (int I) 17 {18/* if (a [I]. date = k) 19 {20 cout <I; 21 return; 22} 23 else24 {*/25 if (a [I]. lchild! = 0) 26 {27 zx (a [I]. lchild); 28} 29 // else now ++; 30 if (a [I]. date = k) 31 {32 cout <now + 1; 33 return; 34} 35 else now ++; 36 if (a [I]. rchild! = 0) 37 {38 zx (a [I]. rchild); 39} 40 // else now ++; 41 42 //} 43} 44 int main () 45 {46 47 48 cin >>> n> k; 49 // for (int I = 1; I <= n; I ++) 50 // a [I]. wz = I; 51 for (int I = 1; I <= n; I ++) 52 {53 cin> a [I]. date; 54 cin> a [I]. lchild; 55 a [a [I]. lchild]. parent = I; 56 cin> a [I]. rchild; 57 a [a [I]. rchild]. parent = I; 58} 59 zx (1); 60 return 0; 61}

 

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.