Skip to content

Instantly share code, notes, and snippets.

@ttfkam
Created July 6, 2015 18:41
Show Gist options
  • Save ttfkam/5229aa2b71aee1df0c03 to your computer and use it in GitHub Desktop.
Save ttfkam/5229aa2b71aee1df0c03 to your computer and use it in GitHub Desktop.
using namespace std;
struct Node {
int value;
node* left;
node* right;
}
boolean findSum(Node* ref, int prev, int target) {
if (!ref) {
return false;
}
int sum = ref->value + prev;
if (sum == target || findSum(ref->left, sum, target) || findSum(ref->right, sum, target)) {
cout << ref.value;
return true;
}
return false;
}
int target = 5;
if (!findSum(head, 0, target)) {
cerr << "Value << target << " is not in the tree" << endl;
}
/*
3: 1,4,9 ; 1,7,12
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment