// Output all the paths from the root node to the leaf node in the binary tree. cpp: defines the entry point of the console application. # Include "stdafx. h "# include <iostream >#include <vector> using namespace std; struct BTNode {char m_value; BTNode * m_left; BTNode * m_right ;}; // create a binary tree void CreatBTree (BTNode * & root) {char nValue = 0; cin> nValue; if ('#' = nValue) {return ;} else {root = new BTNode (); root-> m_value = nValue; CreatBTree (root-> m_left); CreatBTree (root-> m_right );}} // output all paths from the root node to the leaf node in the binary tree (recursion) void FindAllPath (BTNode * pRoot, vector <cha R> path) {if (pRoot! = NULL) {path. push_back (pRoot-> m_value); if (pRoot-> m_left = NULL & pRoot-> m_right = NULL) {for (vector <char> :: iterator iter = path. begin (); iter! = Path. end (); iter ++) {cout <* iter <";}cout <endl; return ;}else {FindAllPath (pRoot-> m_left, path ); findAllPath (pRoot-> m_right, path) ;}} int _ tmain (int argc, _ TCHAR * argv []) {BTNode * pRoot = NULL; vector <char> path; creatBTree (pRoot); cout <"all the paths from the root tree to the leaf node are as follows:" <endl; FindAllPath (pRoot, path); system ("pause "); return 0 ;}
Running result: