/ * Title: input an integer and a binary tree, and print all paths from the root node to the leaf node for an integer. * / / * Thinking: preorder, depth traversal. Start from the root, and the recording path, traversed to a leaf node, and if the desired value is output. Fallback node, looking for the next leaf node until you traverse the path to all leaf nodes. * / #Include <the iostream> #include <string.h> #include <algorithm> #include <the cmath> #include <stdio.h> #include <Vector> #include <Stack> #include <Queue> the using namespace STD; the TreeNode {struct int Val; struct the TreeNode * left; struct the TreeNode * right; the TreeNode (int X): Val (X), left (NULL), right (NULL) { } }; Vector <Vector <int >> RES; void FindPathCore(TreeNode* root,int expectNumber,int currentNumber){ path.push_back(root->val); currentNumber += root->val; bool isLeaf = (root->left == nullptr && root->right == nullptr) ? true: false; if(currentNumber == expectNumber && isLeaf){ res.push_back(path); } if(root->left != nullptr){ FindPathCore(root->left,expectNumber,currentNumber); } if(root->right != nullptr){ FindPathCore(root->right,expectNumber,currentNumber); } path.pop_back(); } vector<vector<int> > FindPath(TreeNode* root,int expectNumber) { if(root == nullptr) return res; vector<int> path; int currentNumber = 0; FindPathCore(root, expectNumber, currentNumber); return res; }