Given a Binary Tree where every node has following structure.
struct node { int key; struct node *left,*right,*random; }
The random pointer points to any random node of the binary tree and can even point to NULL, clone the given binary tree.
struct node { int key; struct node *left,*right,*random; }
int print(node* n, int &count) { if (!n) return -1; int height = print(n->left, count) + 1; printf("%d,%d,%d", n->data, count++, height); print(n->right, count); return height; } print(root, 0);
10
/ \
5 20
\7 | \
15 30
10 5 7 20 15 30
10 5 20 7 15 30
10 5 20 15 7 30
10 5 20 15 30 7
10 20 5 7 15 30
10 20 5 15 7 30
10 20 5 15 30 7
10 20 15 5 7 30
10 20 15 5 30 7
10 20 15 30 5 7
l x r x INT(|L|, |R|)
Ord(15) = 1
Ord(30) = 1
Ord(20) = 1 x 1 x INT(1, 1) = 2 ; INT(1, 1) = 2! / 1 = 2
Ord(7) = 1
Ord(5) = 1
Ord(10) = 1 x 2 x INT(2, 3) = 2 x 5! / (2! x 3!) = 2 x 120 / 12 = 2 x 10 = 20
Useful Link:
http://stackoverflow.com/questions/1701612/permutations-of-bst?rq=1
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *left;
struct node *right;
};
int isAncestor(struct node* root, int n)
{
if(root == NULL)
return 0;
if(root->data == n)
return 1;
if(isAncestor(root->left, n) || isAncestor(root->right, n))
{
printf("%d\n", root->data);
return 1;
}
return 0;
}
struct node* newnode(int data)
{
struct node* node = (struct node*)
malloc(sizeof(struct node));
node->data = data;
node->left = NULL;
node->right = NULL;
// node->nextRight = NULL;
return(node);
}
int main()
{
/* Constructed binary tree is
10
/ \
8 2
/ \
3 90
\
14
*/
struct node *root = newnode(10);
root->left = newnode(8);
root->right = newnode(2);
root->left->left = newnode(3);
root->right->right = newnode(90);
root->right->right->left = newnode(14);
isAncestor(root, 14);
getchar();
return 0;
}
int maxZigzag(struct node* root)
{
int lcount = 0;
int rcount = 0;
int max = 0;
struct node* temp = root;
if(root == NULL)
return 0;
while(1)
{
if(temp->left != NULL)
{
lcount++;
temp = temp->left;
}
else
break;
if(temp->right != NULL)
{
lcount++;
temp = temp->right;
}
else
break;
}
while(1)
{
if(temp->right != NULL)
{
rcount++;
temp = temp->right;
}
else
break;
if(temp->left != NULL)
{
rcount++;
temp = temp->left;
}
else
break;
}
max = MAX(lcount, rcount);
max = MAX(max, maxZigzag(root->left));
max = MAX(max, maxZigzag(root->right));
return max;
}
int main()
{
struct node *root;
insert(&root, 100);
insert(&root, 50);
insert(&root, 25);
insert(&root, 40);
insert(&root, 30);
insert(&root, 35);
insert(&root, 120);
insert(&root, 110);
insert(&root, 32);
printf("maxZigzag = %d\n", maxZigzag(root));
return 0;
}
1 / \ 2 3 / \ / \ 4 5 6 7
Vertical-1: nodes-4 => vertical sum is 4 Vertical-2: nodes-2 => vertical sum is 2 Vertical-3: nodes-1,5,6 => vertical sum is 1+5+6 = 12 Vertical-4: nodes-3 => vertical sum is 3 Vertical-5: nodes-7 => vertical sum is 7
#include <stdio.h>
#include <stdlib.h>
#define MAX_WIDTH 100
struct node
{
int data;
struct node* left;
struct node* right;
};
int height(struct node* node)
{
if (node==NULL)
return 0;
else
{
int lheight = height(node->left);
int rheight = height(node->right);
if (lheight > rheight)
return(lheight+1);
else return(rheight+1);
}
}
void compute_vertical_sum(struct node *node, int index,int vertical_sum[])
{
if( node == NULL ) return;
vertical_sum[index] += node->data;
compute_vertical_sum( node->left, index-1,vertical_sum);
compute_vertical_sum( node->right, index+1,vertical_sum);
}
struct node* newNode(int data)
{
struct node* node = (struct node*)
malloc(sizeof(struct node));
node->data = data;
node->left = NULL;
node->right = NULL;
return(node);
}
int main()
{
int vertical_sum[MAX_WIDTH]= {0};
struct node *root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
root->left->right->left = newNode(6);
root->left->left->left = newNode(9);
int h=height(root);
int max_width_possible=2*h+1;
compute_vertical_sum(root, h,vertical_sum);
for(int i=0; i <max_width_possible; i++)
if ( vertical_sum[i] > 0 )
printf("%d ",vertical_sum[i]);
getchar();
return 0;
}
int find_y_in_xz_path(Tree t, Node *y, Node *z, int yfound) { if(!t) return 0; if(t == z) { return yfound; } else if(t == y) { yfound = 1; } if(find_y_in_xz_path(t->left, y, z, yfound)) return 1; return find_y_in_xz_path(t->right, y, z, yfound); } int main() { find_y_in_xz_path(x,y,z,0); }
Input: pre[] = {20, 10, 11, 13, 12} Output: Yes The give array represents following BST. In the following BST, every internal node has exactly 1 child. Therefor, the output is true. 20 / 10 \ 11 \ 13 /