-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommon.cpp
129 lines (123 loc) · 2.75 KB
/
common.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*Given two Binary Search Trees, find common nodes in them. In other words, find intersection of two BSTs.*/
#include<bits/stdc++.h>
using namespace std;
typedef struct TreeNode{
int val;
struct TreeNode *left;
struct TreeNode *right;
struct TreeNode *next;
}nd;
struct TreeNode* newNode(int data)
{
TreeNode *ptr=(TreeNode*)malloc(sizeof(TreeNode));
ptr->val=data;
ptr->left=NULL;
ptr->right=NULL;
return ptr;
}
void inorder(TreeNode *root)
{
if(root==NULL)return;
inorder(root->left);
cout<<root->val<<endl;
inorder(root->right);
}
TreeNode *bst(TreeNode *root,int val)
{
TreeNode *ptr=newNode(val);
if(!root)
return ptr;
TreeNode *temp=root,*t;
while(root)
{
t=root;
if(val>root->val)
root=root->right;
else if(val<root->val)
root=root->left;
}
if(val>t->val)
t->right=ptr;
else
t->left=ptr;
return temp;
}
void KthLargest(TreeNode *root,int &count,int k){
if(!root || count>k)return;
KthLargest(root->right,count,k);
count++;
if(count==k){
cout<<root->val<<endl;
return ;
}
KthLargest(root->left,count,k);
}
int reverseInorder(TreeNode *root,int &count,int k){
if(!root)return 0;
int r=reverseInorder(root->right,count,k);
if(r)return r;
count++;
if(count==k)return root->val;
return reverseInorder(root->left,count,k);
}
void printCommon(TreeNode *root1,TreeNode *root2){
if(!root1 || !root2)return;
stack<TreeNode*>s1,s2;
while(1){
while(root1){
s1.push(root1);
root1=root1->left;
}
while(root2){
s2.push(root2);
root2=root2->left;
}
if((!s1.empty() && !s2.empty())){
root1 = s1.top();
root2 = s2.top();
if(root1->val == root2->val){
cout<<root1->val<<" ";
s1.pop();
root1=root1->right;
s2.pop();
root2=root2->right;
}
else if(root1->val < root2->val){
s1.pop();
root1 = root1->right;
root2=NULL;
}
else{
s2.pop();
root2=root2->right;
root1=NULL;
}
}else
break;
}
}
int main()
{
TreeNode *root1=NULL;
while(1)
{
int val;
cin>>val;
if(val==-1)break;
root1=bst(root1,val);
}
inorder(root1);
cout<<endl;
TreeNode *root2=NULL;
while(1)
{
int val;
cin>>val;
if(val==-1)break;
root2=bst(root2,val);
}
inorder(root2);
cout<<endl;
printCommon(root1,root2);
return 0;
}