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}