二叉树的遍历游走

  1. 前序,中序,后序遍历的非递归实现。
  2. 层次遍历,从上到下或从下到上,从左到右或从右到左,只输出叶子节点,只输出某一层等等。

1、代码:

 1 void PreOrder(BinaryTreeNode* root)
 2 {
 3     if (!root) return;
 4     stack<BinaryTreeNode*> nodes;
 5     while (root != NULL || !nodes.empty())
 6     {
 7         if (root != NULL)
 8         {
 9             cout << root->m_nValue << " ";
10             nodes.push(root);
11             root = root->m_pLeft;
12         }
13         else
14         {
15             root = nodes.top();
16             nodes.pop();
17             root = root->m_pRight;
18         }
19     }
20     cout << endl;
21 }
22 
23 void InOrder(BinaryTreeNode* root)
24 {
25     if (!root) return;
26     stack<BinaryTreeNode*> nodes;
27     while (root != NULL || !nodes.empty())
28     {
29         if (root != NULL)
30         {
31             nodes.push(root);
32             root = root->m_pLeft;
33         }
34         else
35         {
36             root = nodes.top();
37             nodes.pop();
38             cout << root->m_nValue << " ";
39             root = root->m_pRight;
40         }
41     }
42     cout << endl;
43 }
44 
45 void PostOrder(BinaryTreeNode* root)
46 {
47     if (!root) return;
48     stack<BinaryTreeNode*> nodes1, nodes2;
49     BinaryTreeNode* node;
50     nodes1.push(root);
51     while (!nodes1.empty())
52     {
53         node = nodes1.top();
54         nodes1.pop();
55         nodes2.push(node);
56         if (node->m_pLeft)
57             nodes1.push(node->m_pLeft);
58         if (node->m_pRight)
59             nodes1.push(node->m_pRight);
60     }
61     while (!nodes2.empty())
62     {
63         cout << nodes2.top()->m_nValue << " ";
64         nodes2.pop();
65     }
66     cout << endl;
67 }

 

2、代码:

 1 void TravelByLevel(BinaryTreeNode* root, bool direction)
 2 {
 3     if (root == NULL) return;
 4     int cur = 0, last;
 5     vector<BinaryTreeNode*> vec;
 6     vec.push_back(root);
 7     while (cur < vec.size())
 8     {
 9         last = vec.size();
10         vec.push_back(NULL);
11         while (cur < last)
12         {
13             if (direction)
14             {
15                 if (vec[cur]->m_pLeft)
16                     vec.push_back(vec[cur]->m_pLeft);
17                 if (vec[cur]->m_pRight)
18                     vec.push_back(vec[cur]->m_pRight);
19             }
20             else
21             {
22                 if (vec[cur]->m_pRight)
23                     vec.push_back(vec[cur]->m_pRight);
24                 if (vec[cur]->m_pLeft)
25                     vec.push_back(vec[cur]->m_pLeft);
26             }
27             cur++;
28         }
29         cur++; //跳过NULL
30     }
31     vector<BinaryTreeNode*>::reverse_iterator riter = vec.rbegin();
32     for (; riter != vec.rend(); riter++)
33     {
34         if (*riter == NULL)
35             cout << endl;
36         else
37             cout << (*riter)->m_nValue << " ";
38     }
39     cout << endl;
40 }
41 
42 void BFS_level(BinaryTreeNode* root)
43 {
44     if (root == NULL) return;
45     deque<BinaryTreeNode*> dq;
46     BinaryTreeNode* last = root;
47     while (true)
48     {
49         if (root->m_pLeft) dq.push_back(root->m_pLeft);
50         if (root->m_pRight) dq.push_back(root->m_pRight);
51         cout << root->m_nValue << (root == last ? "\n" : " ");
52         if (root == last)
53         {
54             if (dq.empty()) break;
55             last = dq.back();
56         }
57         root = dq.front();
58         dq.pop_front();
59     }
60 }
61 
62 void BFS_level_nth(BinaryTreeNode* root, int level)
63 {
64     if (root == NULL || level < 0) return;
65     deque<BinaryTreeNode*> dq;
66     BinaryTreeNode* last = root;
67     while (true)
68     {
69         if (root->m_pLeft) dq.push_back(root->m_pLeft);
70         if (root->m_pRight) dq.push_back(root->m_pRight);
71         if (level == 0)
72             cout << root->m_nValue << (root == last ? "\n" : " ");
73         if (root == last)
74         {
75             if (--level < 0 || dq.empty()) break;
76             last = dq.back();
77         }
78         root = dq.front();
79         dq.pop_front();
80     }
81 }

 

    原文作者:算法小白
    原文地址: https://www.cnblogs.com/wangpengjie/archive/2013/06/07/3125188.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞