|
| 1 | +/* |
| 2 | +/********************************************************** |
| 3 | + |
| 4 | + Following is the Binary Tree Node class structure |
| 5 | + |
| 6 | + template <typename T> |
| 7 | + class BinaryTreeNode { |
| 8 | + public: |
| 9 | + T data; |
| 10 | + BinaryTreeNode<T> *left; |
| 11 | + BinaryTreeNode<T> *right; |
| 12 | + |
| 13 | + BinaryTreeNode(T data) { |
| 14 | + this->data = data; |
| 15 | + left = NULL; |
| 16 | + right = NULL; |
| 17 | + } |
| 18 | + }; |
| 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 | + |
| 39 | +***********************************************************/ |
| 40 | +#include<algorithm> |
| 41 | +#include<vector> |
| 42 | +int k=0; |
| 43 | +void convert(BinaryTreeNode<int>* root,int* ans){ |
| 44 | + if(root==NULL){ |
| 45 | + return; |
| 46 | + } |
| 47 | + ans[k]=root->data; |
| 48 | + k++; |
| 49 | + convert(root->left,ans); |
| 50 | + convert(root->right,ans); |
| 51 | +} |
| 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){ |
| 117 | + if(root==NULL){ |
| 118 | + return; |
| 119 | + } |
| 120 | + ans[k]=root->data; |
| 121 | + k++; |
| 122 | + convert(root->left,ans); |
| 123 | + convert(root->right,ans); |
| 124 | +} |
| 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 | +} |
0 commit comments