标签:code ret 打印 log 描述 应该 使用 path else
1 /*struct TreeNode { 2 int val; 3 struct TreeNode *left; 4 struct TreeNode *right; 5 TreeNode(int x) : 6 val(x), left(NULL), right(NULL) { 7 } 8 };*/ 9 class Solution { 10 public: 11 vector<vector<int> > FindPath(TreeNode* root,int expectNumber) { 12 vector<int> tmp; 13 vector<vector<int> >result; 14 if(root == NULL || expectNumber<=0) 15 return result; 16 IsPath(root,expectNumber,result,tmp); 17 return result; 18 } 19 void IsPath(TreeNode* root,int expectNumber,vector<vector<int> > &findPath,vector<int> &path)//不是取地址的话,结果不正确 20 { 21 path.push_back(root->val); 22 if(root == NULL) 23 return; 24 bool isLeaf = (root->left == NULL)&&(root->right == NULL); 25 if((root->val == expectNumber)&&isLeaf) 26 { 27 findPath.push_back(path); 28 } 29 else 30 { 31 if(root->left != NULL) IsPath(root->left,expectNumber-root->val,findPath,path); 32 if(root->right != NULL) IsPath(root->right,expectNumber-root->val,findPath,path); 33 } 34 path.pop_back(); 35 } 36 };
1 vector<vector<int> > r; 2 r = s.FindPath(T1,n); 3 4 for(int i=0;i<r.size();i++) 5 { 6 for(int j=0;j<r[i].size();j++) 7 { 8 cout<<r[i][j]<<" "; 9 } 10 cout<<endl; 11 }
标签:code ret 打印 log 描述 应该 使用 path else
原文地址:http://www.cnblogs.com/qqky/p/6910597.html