13,251,760 members (62,723 online)

# C++ binary tree problem

```#include<iostream>
using namespace std;
struct treeNode{
int indexI,indexJ;
treeNode *Right,*Down;
};
treeNode*Try(int i,int j,int n,int x=0);
void preorderPrint( treeNode *root );
int**Array;
for(int i=0;i<n;i++)>
for(int j=0;j<n;j++){>
cout<<"\nEnter the element["<<i<<"]["<<j<<"]: ";
cin>>A[i][j];
}
}
void main(){
int size;
cout<<"\nEnter the size of the array : ";
cin>>size;
Array=new int*[size];
for(int i=0;i<size;i++)>
Array[i]=new int[size];
treeNode*root=Try(0,0,size);
preorderPrint( root );
}
treeNode*Try(int i,int j,int n,int x){
treeNode*p=new treeNode;
if(Array[i][j]!=-1){
bool b=false;
if(x==0){
if((Array[i][j]+j)<n){>
p->indexI=i; p->indexJ=j;
b=true;}
}
else{
if((Array[i][j]+i)<n){>
p->indexI=i; p->indexJ=j;
b=true;}
}
if(b){
p->Right=Try(i,j+Array[i][j],n,0);
p->Down=Try(i+Array[i][j],j,n,x=1);
return p;
}
else
return NULL;
}

}
void preorderPrint( treeNode *root ) {
// Print all the items in the tree to which root points.
// The item in the root is printed first, followed by the
// items in the left subtree and then the items in the
// right subtree.
if ( root != NULL ) {  // (Otherwise, there's nothing to print.)
cout << root->indexI << " "<<root->Right<<" ";      // Print the root item.
preorderPrint( root->Down );    // Print items in left subtree.
preorderPrint( root->Right );   // Print items in right subtree.
}
} // end preorderPrint()```

The program is throwing exceptions and I do not know why.
Tags: C++

## Preview

2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the The Code Project Open License (CPOL).