#include <iostream>
#include <vector>

using namespace std;

struct TNode {
  int m_nVal;
  TNode *m_pLeft;
  TNode *m_pRight;
};

void findPath(TNode *pTree, vector<int>& path, const int expectedSum, int& currentSum) {
  if(pTree==NULL)
    return;

  currentSum += pTree->m_nVal;
  path.push_back(pTree->m_nVal);

  bool isLeave = (!pTree->m_pLeft) && (!pTree->m_pRight);
  if(isLeave && currentSum==expectedSum) {
    for(vector<int>::const_iterator iter = path.begin(); iter != path.end(); iter++)
      cout << *iter << "\t";
    cout << endl;
  }

  if(pTree->m_pLeft)
    findPath(pTree->m_pLeft, path, expectedSum, currentSum);

  if(pTree->m_pRight)
    findPath(pTree->m_pRight, path, expectedSum, currentSum);

  currentSum -= pTree->m_nVal;
  path.pop_back();
}

int main(int argc, char **argv) {

  TNode *root = new TNode;
  root->m_nVal = 10;

  TNode *left1 = new TNode;
  left1->m_nVal = 5;
  TNode *right1 = new TNode;
  right1->m_nVal = 12;
  root->m_pLeft = left1;
  root->m_pRight = right1;

  TNode *left2 = new TNode;
  left2->m_nVal = 4;
  TNode *right2 = new TNode;
  right2->m_nVal = 7;
  left1->m_pLeft = left2;
  left1->m_pRight = right2;

  vector<int> path;
  int sum = 0;
  findPath(root, path, 19, sum);

  return 0;
}
