|
1 | | -/* |
2 | 1 | /**********************************************************
|
3 | 2 |
|
4 | 3 | Following is the Binary Tree Node class structure
|
|
16 | 15 | right = NULL;
|
17 | 16 | }
|
18 | 17 | };
|
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 | | - }; |
38 | 18 |
|
39 | 19 | ***********************************************************/
|
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){ |
44 | 21 | if(root==NULL){
|
45 | | - return; |
| 22 | + return 0; |
46 | 23 | }
|
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; |
51 | 29 | }
|
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) { |
117 | 31 | if(root==NULL){
|
118 | 32 | return;
|
119 | 33 | }
|
120 | | - ans[k]=root->data; |
121 | | - k++; |
122 | | - convert(root->left,ans); |
123 | | - convert(root->right,ans); |
| 34 | + int ans=replace(root,0); |
124 | 35 | }
|
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 | | - } |
144 | 36 | }
|
0 commit comments