#include
<
iostream
>
#include < vector >
#include < cstdlib >
using namespace std;
struct Node
{
char data;
Node * left;
Node * right;
};
enum Tag{goLeft, goRight, goBack };
/* 打印从根到叶子结点之路径 */
void PrintPath( const vector < Node *> & v )
{
vector < Node *> ::const_iterator vi;
for ( vi = v.begin(); vi != v.end(); ++ vi )
{
Node * n = reinterpret_cast < Node *> ( * vi);
cout << n -> data << " " ;
}
cout << endl;
}
void PrintAllPaths(Node * root)
{
assert(root != NULL);
vector < Node *> vec_node;
vector < Tag > vec_flag;
// init
vec_node.push_back(root);
vec_flag.push_back(goLeft);
while ( ! vec_node.empty())
{
Node * curNode = vec_node.back();
Tag curFlag = vec_flag.back();
switch (curFlag)
{
case goLeft:
vec_flag.pop_back();
vec_flag.push_back(goRight);
if (curNode -> left != NULL)
{
vec_node.push_back(curNode -> left);
vec_flag.push_back(goLeft);
}
break ;
case goRight:
vec_flag.pop_back();
vec_flag.push_back(goBack);
if (curNode -> right != NULL)
{
vec_node.push_back(curNode -> right);
vec_flag.push_back(goLeft);
}
break ;
case goBack:
if (NULL == curNode -> left && NULL == curNode -> right)
PrintPath(vec_node);
vec_flag.pop_back();
vec_node.pop_back();
break ;
default :
break ;
} // switch-end
} // while-end
}
int main()
{
Node root, b, c, d, e, f, g, h;
root.data = ' a ' ;
root.left =& b;
root.right =& e;
b.data = ' b ' ;
b.left =& c;
b.right =& d;
c.data = ' c ' ;
c.left = NULL;
c.right = NULL;
d.data = ' d ' ;
d.left = NULL;
d.right = NULL;
e.data = ' e ' ;
e.left =& f;
e.right =& g;
f.data = ' f ' ;
f.left = NULL;
f.right = NULL;
g.data = ' g ' ;
g.left = NULL;
g.right =& h;
h.data = ' h ' ;
h.left = NULL;
h.right = NULL;
PrintAllPaths( & root);
system( " PAUSE " );
return 0 ;
}
#include < vector >
#include < cstdlib >
using namespace std;
struct Node
{
char data;
Node * left;
Node * right;
};
enum Tag{goLeft, goRight, goBack };
/* 打印从根到叶子结点之路径 */
void PrintPath( const vector < Node *> & v )
{
vector < Node *> ::const_iterator vi;
for ( vi = v.begin(); vi != v.end(); ++ vi )
{
Node * n = reinterpret_cast < Node *> ( * vi);
cout << n -> data << " " ;
}
cout << endl;
}
void PrintAllPaths(Node * root)
{
assert(root != NULL);
vector < Node *> vec_node;
vector < Tag > vec_flag;
// init
vec_node.push_back(root);
vec_flag.push_back(goLeft);
while ( ! vec_node.empty())
{
Node * curNode = vec_node.back();
Tag curFlag = vec_flag.back();
switch (curFlag)
{
case goLeft:
vec_flag.pop_back();
vec_flag.push_back(goRight);
if (curNode -> left != NULL)
{
vec_node.push_back(curNode -> left);
vec_flag.push_back(goLeft);
}
break ;
case goRight:
vec_flag.pop_back();
vec_flag.push_back(goBack);
if (curNode -> right != NULL)
{
vec_node.push_back(curNode -> right);
vec_flag.push_back(goLeft);
}
break ;
case goBack:
if (NULL == curNode -> left && NULL == curNode -> right)
PrintPath(vec_node);
vec_flag.pop_back();
vec_node.pop_back();
break ;
default :
break ;
} // switch-end
} // while-end
}
int main()
{
Node root, b, c, d, e, f, g, h;
root.data = ' a ' ;
root.left =& b;
root.right =& e;
b.data = ' b ' ;
b.left =& c;
b.right =& d;
c.data = ' c ' ;
c.left = NULL;
c.right = NULL;
d.data = ' d ' ;
d.left = NULL;
d.right = NULL;
e.data = ' e ' ;
e.left =& f;
e.right =& g;
f.data = ' f ' ;
f.left = NULL;
f.right = NULL;
g.data = ' g ' ;
g.left = NULL;
g.right =& h;
h.data = ' h ' ;
h.left = NULL;
h.right = NULL;
PrintAllPaths( & root);
system( " PAUSE " );
return 0 ;
}