输入一颗二叉树的根节点和一个整数,打印出二叉树中结点值的和为输入整数的所有路径。路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径。(注意: 在返回值的list中,数组长度大的数组靠前)
/* struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) { } };*/ class Solution { public: void search_tree(vector<vector<int>> &vv, vector<int> v , TreeNode *root, int sum, int expectNumber) { v.push_back(root->val); sum += root->val; if(root->left == nullptr && root->right == nullptr) { if(sum == expectNumber)vv.push_back(v); return; } if(root->left) search_tree(vv, v, root->left, sum, expectNumber); if(root->right) search_tree(vv, v, root->right, sum, expectNumber); } vector<vector<int> > FindPath(TreeNode* root,int expectNumber) { if(root == nullptr)return vector<vector<int> >(); vector<vector<int> > vv; vector<int> v; search_tree(vv, v, root, 0, expectNumber); sort(vv.begin(), vv.end(), [](vector<int> &a,vector<int> &b) {return a.size()>b.size();}); return vv; } };