OJ_find binary tree

#include<iostream>
using namespace std;
int n,m;
int d[120];
int t=1;
int re;
struct Node
{
 int data;
 int left;
 int right;
}node[120];

void zhao(int k)
{
 if(node[k].data==m) re=k;
 if(node[k].left!=0) zhao(node[k].left);
 d[k]=t;
 t++;
 if(node[k].right!=0) zhao(node[k].right);

int main()
{

 int i;
 int da,r,l;
 cin>>n>>m;
 for(i=1;i<=n;i++)
 {
  cin>>da>>l>>r;
  node[i].data=da;
  node[i].left=l;
  node[i].right=r;
 }
 zhao(1);
 cout<<d[re]<<endl;
}

 

Find elements by pseudo-tree construction, and there is a section in the middle that is more complicated to understand

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325126001&siteId=291194637