Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1fec497

Browse files
Update Pair sum in a BST
1 parent 966c545 commit 1fec497

File tree

1 file changed

+9
-117
lines changed

1 file changed

+9
-117
lines changed

‎BST/Pair sum in a BST

Lines changed: 9 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/*
21
/**********************************************************
32

43
Following is the Binary Tree Node class structure
@@ -16,129 +15,22 @@
1615
right = NULL;
1716
}
1817
};
19-
20-
***********************************************************/
21-
22-
/**********************************************************
23-
Following is the Binary Tree Node class structure
24-
25-
template <typename T>
26-
class BinaryTreeNode {
27-
public :
28-
T data;
29-
BinaryTreeNode<T> *left;
30-
BinaryTreeNode<T> *right;
31-
32-
BinaryTreeNode(T data) {
33-
this -> data = data;
34-
left = NULL;
35-
right = NULL;
36-
}
37-
};
3818

3919
***********************************************************/
40-
#include<algorithm>
41-
#include<vector>
42-
int k=0;
43-
void convert(BinaryTreeNode<int>* root,int* ans){
20+
int replace(BinaryTreeNode<int>* root,int sum){
4421
if(root==NULL){
45-
return;
22+
return 0;
4623
}
47-
ans[k]=root->data;
48-
k++;
49-
convert(root->left,ans);
50-
convert(root->right,ans);
24+
int rightsum=replace(root->right,sum);
25+
int prevroot=root->data;
26+
root->data=sum+rightsum+root->data;
27+
int leftsum=replace(root->left,rightsum+sum+prevroot);
28+
return leftsum+rightsum+prevroot;
5129
}
52-
53-
void printNodesSumToS(BinaryTreeNode<int> *root, int sum) {
54-
int ans[100000000];
55-
convert(root,ans);
56-
sort(ans,ans+k);
57-
int i=0;
58-
int j=k-1;
59-
while(i<j){
60-
if(ans[i]+ans[j]==sum){
61-
cout<<ans[i]<<" "<<ans[j]<<endl;
62-
i++;j--;
63-
}
64-
else if(ans[i]+ans[j]>sum){
65-
j--;
66-
}
67-
else if(ans[i]+ans[j]<sum){
68-
i++;
69-
}
70-
}
71-
}
72-
/*
73-
//O(N) time complexity O(N) Space Complexity
74-
//O(N) O(NlogN) space complexity solution
75-
/**********************************************************
76-
77-
Following is the Binary Tree Node class structure
78-
79-
template <typename T>
80-
class BinaryTreeNode {
81-
public:
82-
T data;
83-
BinaryTreeNode<T> *left;
84-
BinaryTreeNode<T> *right;
85-
86-
BinaryTreeNode(T data) {
87-
this->data = data;
88-
left = NULL;
89-
right = NULL;
90-
}
91-
};
92-
93-
***********************************************************/
94-
95-
/**********************************************************
96-
Following is the Binary Tree Node class structure
97-
98-
template <typename T>
99-
class BinaryTreeNode {
100-
public :
101-
T data;
102-
BinaryTreeNode<T> *left;
103-
BinaryTreeNode<T> *right;
104-
105-
BinaryTreeNode(T data) {
106-
this -> data = data;
107-
left = NULL;
108-
right = NULL;
109-
}
110-
};
111-
112-
***********************************************************/
113-
#include<algorithm>
114-
#include<vector>
115-
int k=0;
116-
void convert(BinaryTreeNode<int>* root,int* ans){
30+
void replaceWithLargerNodesSum(BinaryTreeNode<int> *root) {
11731
if(root==NULL){
11832
return;
11933
}
120-
ans[k]=root->data;
121-
k++;
122-
convert(root->left,ans);
123-
convert(root->right,ans);
34+
int ans=replace(root,0);
12435
}
125-
126-
void printNodesSumToS(BinaryTreeNode<int> *root, int sum) {
127-
int ans[100000000];
128-
convert(root,ans);
129-
sort(ans,ans+k);
130-
int i=0;
131-
int j=k-1;
132-
while(i<j){
133-
if(ans[i]+ans[j]==sum){
134-
cout<<ans[i]<<" "<<ans[j]<<endl;
135-
i++;j--;
136-
}
137-
else if(ans[i]+ans[j]>sum){
138-
j--;
139-
}
140-
else if(ans[i]+ans[j]<sum){
141-
i++;
142-
}
143-
}
14436
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /