class node
{
public:
int data;
struct node *left,*right,*random;
bool isThreaded ;
};
node* newNode(int data)
{
node* neNode = new node;
neNode->data = data;
neNode->left = NULL;
neNode->right = NULL;
neNode->isThreaded =false;
return neNode;
}
void inorder(node* root)
{
if(!root) return;
inorder(root->left);
cout<<root->data<<" ";
inorder(root->right);
}
int height(node* root)
{
if(root==NULL) return 0;
int lHeight = height(root->left);
int rHeight = height(root->right);
return lHeight>rHeight?lHeight+1:rHeight+1;
}
int main()
{
node *root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
//root->left->left = newNode(1);
//root->left->right = newNode(5);
root->right->left = newNode(4);
root->right->left->left = newNode(6);
root->right->left->left->left = newNode(8);
root->right->left->left->right = newNode(9);
root->right->right = newNode(5);
//root->right->right->left = newNode(9);
root->right->right->right = newNode(7);
root->right->right->right->left = newNode(10);
cout<<closestLeafDistance(root,5);
}
Wednesday, 5 August 2015
Binary Tree Basic Functions
Labels:
binary tree
Subscribe to:
Post Comments (Atom)
Check Comment
ReplyDelete