标签:for pac 所有路径 ios size main include space 输出
输出二叉树中所有从根结点到叶子结点的路径
#include <iostream> #include <vector> using namespace std; struct BiTNode { char m_value; BiTNode *m_left; BiTNode *m_right; }; //先序创建二叉树 void CreatBiTree(BiTNode *&root) { char nValue = 0; cin >> nValue; if (‘#‘ == nValue) { return; } else { root = new BiTNode(); root->m_value = nValue; CreatBiTree(root->m_left); CreatBiTree(root->m_right); } } //输出二叉树中所有从根结点到叶子结点的路径(递归) void FindAllPath(BiTNode *pRoot, vector<char> 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 main() { BiTNode *pRoot = NULL; vector<char> path; CreatBiTree(pRoot); cout << "二叉树中从根到叶子结点的所有路径如下:" << endl; FindAllPath(pRoot, path); system("pause"); return 0; }
标签:for pac 所有路径 ios size main include space 输出
原文地址:http://www.cnblogs.com/xinxin521/p/6127671.html