【二叉树遍历模版】前序遍历
 
 
1.递归实现
test.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
 
#include <iostream>
#include <cstdio>
#include <stack>
#include <vector>
#include "BinaryTree.h"

using namespace std;

void preorder(TreeNode *root, vector<int> &path)
{
    if(root != NULL)
    {
        path.push_back(root->val);
        preorder(root->left, path);
        preorder(root->right, path);
    }
}
vector<int> preorderTraversal(TreeNode *root)
{
    vector<int> path;
    preorder(root, path);
    return path;
}

// 树中结点含有分叉,
//                  8
//              /       \
//             6         1
//           /   \
//          9     2
//               / \
//              4   7
int main()
{
    TreeNode *pNodeA1 = CreateBinaryTreeNode(8);
    TreeNode *pNodeA2 = CreateBinaryTreeNode(6);
    TreeNode *pNodeA3 = CreateBinaryTreeNode(1);
    TreeNode *pNodeA4 = CreateBinaryTreeNode(9);
    TreeNode *pNodeA5 = CreateBinaryTreeNode(2);
    TreeNode *pNodeA6 = CreateBinaryTreeNode(4);
    TreeNode *pNodeA7 = CreateBinaryTreeNode(7);

ConnectTreeNodes(pNodeA1, pNodeA2, pNodeA3);
    ConnectTreeNodes(pNodeA2, pNodeA4, pNodeA5);
    ConnectTreeNodes(pNodeA5, pNodeA6, pNodeA7);

PrintTree(pNodeA1);

vector<int> ans = preorderTraversal(pNodeA1);

for (int i = 0; i < ans.size(); ++i)
    {
        cout << ans[i] << " ";
    }
    cout << endl;

DestroyTree(pNodeA1);
    return 0;
}

 输出结果:
8 6 9 2 4 7 1
 

2.非递归实现(迭代实现)

根据前序遍历访问的顺序,优先访问根结点,然后再分别访问左孩子和右孩子。即对于任一结点,其可看做是根结点,因此可以直接访问,访问完之后,若其左孩子不为空,按相同规则访问它的左子树;当访问其左子树时,再访问它的右子树。因此其处理过程如下:

对于任一结点P:

1)访问结点P,并将结点P入栈;

2)判断结点P的左孩子是否为空,若为空,则取栈顶结点并进行出栈操作,并将栈顶结点的右孩子置为当前的结点P,循环至1);若不为空,则将P的左孩子置为当前的结点P;

3)直到P为NULL并且栈为空,则遍历结束。

test.cpp:

简单的实现方式
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
 
#include <iostream>
#include <cstdio>
#include <stack>
#include <vector>
#include "BinaryTree.h"

using namespace std;

/**
 * Definition for binary tree
 * struct TreeNode {
 * int val;
 * TreeNode *left;
 * TreeNode *right;
 * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
vector<int> rootRLTraversal(TreeNode *root)
{
    stack<TreeNode *> s;
    vector<int> ans;
    TreeNode *tmp;
    s.push(root);

while (!s.empty())
    {
        tmp = s.top();
        s.pop();

if (!tmp)
        {
            continue;
        }
        //注意这里tmp不为空的时候才可以把他放进去
        ans.push_back(tmp->val);

s.push(tmp->right);
        s.push(tmp->left);

}
    return ans;
}

// 树中结点含有分叉,
//                  6
//              /       \
//             7         2
//           /   \
//          1     4
//               / \
//              3   5
int main()
{
    TreeNode *pNodeA1 = CreateBinaryTreeNode(6);
    TreeNode *pNodeA2 = CreateBinaryTreeNode(7);
    TreeNode *pNodeA3 = CreateBinaryTreeNode(2);
    TreeNode *pNodeA4 = CreateBinaryTreeNode(1);
    TreeNode *pNodeA5 = CreateBinaryTreeNode(4);
    TreeNode *pNodeA6 = CreateBinaryTreeNode(3);
    TreeNode *pNodeA7 = CreateBinaryTreeNode(5);

ConnectTreeNodes(pNodeA1, pNodeA2, pNodeA3);
    ConnectTreeNodes(pNodeA2, pNodeA4, pNodeA5);
    ConnectTreeNodes(pNodeA5, pNodeA6, pNodeA7);

vector<int> ans = rootRLTraversal(pNodeA1);

for (int i = 0; i < ans.size(); ++i)
    {
        cout << ans[i] << " ";
    }
    cout << endl;

DestroyTree(pNodeA1);
    return 0;
}

输出结果:
6 7 1 4 3 5 2
 

复杂一点但是代码风格和中序和后序一致,

test.cpp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
 
#include <iostream>
#include <cstdio>
#include <stack>
#include <vector>
#include "BinaryTree.h"

using namespace std;

//非递归前序遍历
vector<int> preorderTraversal(TreeNode *root)
{
    stack<TreeNode *> s;
    vector<int> path;
    TreeNode *p = root;
    while(p != NULL || !s.empty())
    {
        while(p != NULL)
        {
            path.push_back(p->val);
            s.push(p);
            p = p->left;
        }
        if(!s.empty())
        {
            p = s.top();
            s.pop();
            p = p->right;
        }
    }
    return path;
}

// 树中结点含有分叉,
//                  8
//              /       \
//             6         1
//           /   \
//          9     2
//               / \
//              4   7
int main()
{
    TreeNode *pNodeA1 = CreateBinaryTreeNode(8);
    TreeNode *pNodeA2 = CreateBinaryTreeNode(6);
    TreeNode *pNodeA3 = CreateBinaryTreeNode(1);
    TreeNode *pNodeA4 = CreateBinaryTreeNode(9);
    TreeNode *pNodeA5 = CreateBinaryTreeNode(2);
    TreeNode *pNodeA6 = CreateBinaryTreeNode(4);
    TreeNode *pNodeA7 = CreateBinaryTreeNode(7);

ConnectTreeNodes(pNodeA1, pNodeA2, pNodeA3);
    ConnectTreeNodes(pNodeA2, pNodeA4, pNodeA5);
    ConnectTreeNodes(pNodeA5, pNodeA6, pNodeA7);

PrintTree(pNodeA1);

vector<int> ans = preorderTraversal(pNodeA1);

for (int i = 0; i < ans.size(); ++i)
    {
        cout << ans[i] << " ";
    }
    cout << endl;

DestroyTree(pNodeA1);
    return 0;
}

输出结果:

8 6 9 2 4 7 1
【二叉树遍历模版】中序遍历
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
 
#include <iostream>
#include <cstdio>
#include <stack>
#include <vector>
#include "BinaryTree.h"

using namespace std;

void inorder(TreeNode *root, vector<int> &path)
{
    if(root != NULL)
    {
        inorder(root->left, path);
        path.push_back(root->val);
        inorder(root->right, path);
    }
}

vector<int> inorderTraversal(TreeNode *root)
{
    vector<int> path;
    inorder(root, path);
    return path;
}

// 树中结点含有分叉,
//                  8
//              /       \
//             6         1
//           /   \
//          9     2
//               / \
//              4   7
int main()
{
    TreeNode *pNodeA1 = CreateBinaryTreeNode(8);
    TreeNode *pNodeA2 = CreateBinaryTreeNode(6);
    TreeNode *pNodeA3 = CreateBinaryTreeNode(1);
    TreeNode *pNodeA4 = CreateBinaryTreeNode(9);
    TreeNode *pNodeA5 = CreateBinaryTreeNode(2);
    TreeNode *pNodeA6 = CreateBinaryTreeNode(4);
    TreeNode *pNodeA7 = CreateBinaryTreeNode(7);

ConnectTreeNodes(pNodeA1, pNodeA2, pNodeA3);
    ConnectTreeNodes(pNodeA2, pNodeA4, pNodeA5);
    ConnectTreeNodes(pNodeA5, pNodeA6, pNodeA7);

PrintTree(pNodeA1);

vector<int> ans = inorderTraversal(pNodeA1);

for (int i = 0; i < ans.size(); ++i)
    {
        cout << ans[i] << " ";
    }
    cout << endl;

DestroyTree(pNodeA1);
    return 0;
}

输出结果:

9 6 4 2 7 8 1
 

2.非递归实现

根据中序遍历的顺序,对于任一结点,优先访问其左孩子,而左孩子结点又可以看做一根结点,然后继续访问其左孩子结点,直到遇到左孩子结点为空的结点才进行访问,然后按相同的规则访问其右子树。因此其处理过程如下:

对于任一结点P,

1)若其左孩子不为空,则将P入栈并将P的左孩子置为当前的P,然后对当前结点P再进行相同的处理;

2)若其左孩子为空,则取栈顶元素并进行出栈操作,访问该栈顶结点,然后将当前的P置为栈顶结点的右孩子;

3)直到P为NULL并且栈为空则遍历结束

test.cpp:

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
 
#include <iostream>
#include <cstdio>
#include <stack>
#include <vector>
#include "BinaryTree.h"

using namespace std;

//非递归中序遍历
vector<int> inorderTraversal(TreeNode *root)
{
    stack<TreeNode *> s;
    vector<int> path;
    TreeNode *p = root;
    while(p != NULL || !s.empty())
    {
        while(p != NULL)
        {
            s.push(p);
            p = p->left;
        }
        if(!s.empty())
        {
            p = s.top();
            path.push_back(p->val);
            s.pop();
            p = p->right;
        }
    }
    return path;
}

// 树中结点含有分叉,
//                  8
//              /       \
//             6         1
//           /   \
//          9     2
//               / \
//              4   7
int main()
{
    TreeNode *pNodeA1 = CreateBinaryTreeNode(8);
    TreeNode *pNodeA2 = CreateBinaryTreeNode(6);
    TreeNode *pNodeA3 = CreateBinaryTreeNode(1);
    TreeNode *pNodeA4 = CreateBinaryTreeNode(9);
    TreeNode *pNodeA5 = CreateBinaryTreeNode(2);
    TreeNode *pNodeA6 = CreateBinaryTreeNode(4);
    TreeNode *pNodeA7 = CreateBinaryTreeNode(7);

ConnectTreeNodes(pNodeA1, pNodeA2, pNodeA3);
    ConnectTreeNodes(pNodeA2, pNodeA4, pNodeA5);
    ConnectTreeNodes(pNodeA5, pNodeA6, pNodeA7);

PrintTree(pNodeA1);

vector<int> ans = inorderTraversal(pNodeA1);

for (int i = 0; i < ans.size(); ++i)
    {
        cout << ans[i] << " ";
    }
    cout << endl;

DestroyTree(pNodeA1);
    return 0;
}

 

输出结果:

9 6 4 2 7 8 1
 
 
【二叉树遍历模版】后序遍历-递归实现
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
 
#include <iostream>
#include <cstdio>
#include <stack>
#include <vector>
#include "BinaryTree.h"

using namespace std;

/**
 * Definition for binary tree
 * struct TreeNode {
 * int val;
 * TreeNode *left;
 * TreeNode *right;
 * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
void postorder(TreeNode *root, vector<int> &path)
{
    if(root != NULL)
    {
        postorder(root->left, path);
        postorder(root->right, path);
        path.push_back(root->val);
    }
}
vector<int> postorderTraversal(TreeNode *root)
{

vector<int> path;
    postorder(root, path);
    return path;
}

// 树中结点含有分叉,
//                  8
//              /       \
//             6         1
//           /   \
//          9     2
//               / \
//              4   7
int main()
{
    TreeNode *pNodeA1 = CreateBinaryTreeNode(8);
    TreeNode *pNodeA2 = CreateBinaryTreeNode(6);
    TreeNode *pNodeA3 = CreateBinaryTreeNode(1);
    TreeNode *pNodeA4 = CreateBinaryTreeNode(9);
    TreeNode *pNodeA5 = CreateBinaryTreeNode(2);
    TreeNode *pNodeA6 = CreateBinaryTreeNode(4);
    TreeNode *pNodeA7 = CreateBinaryTreeNode(7);

ConnectTreeNodes(pNodeA1, pNodeA2, pNodeA3);
    ConnectTreeNodes(pNodeA2, pNodeA4, pNodeA5);
    ConnectTreeNodes(pNodeA5, pNodeA6, pNodeA7);

PrintTree(pNodeA1);

vector<int> ans = postorderTraversal(pNodeA1);

for (int i = 0; i < ans.size(); ++i)
    {
        cout << ans[i] << " ";
    }
    cout << endl;

DestroyTree(pNodeA1);
    return 0;
}

9 4 7 2 6 1 8运行结果:
 

2.非递归实现

后序遍历的非递归实现是三种遍历方式中最难的一种。因为在后序遍历中,要保证左孩子和右孩子都已被访问并且左孩子在右孩子前访问才能访问根结点,这就为流程的控制带来了难题。下面介绍两种思路。

第一种思路:对于任一结点P,将其入栈,然后沿其左子树一直往下搜索,直到搜索到没有左孩子的结点,此时该结点出现在栈顶,但是此时不能将其出栈并访问, 因此其右孩子还为被访问。所以接下来按照相同的规则对其右子树进行相同的处理,当访问完其右孩子时,该结点又出现在栈顶,此时可以将其出栈并访问。这样就 保证了正确的访问顺序。可以看出,在这个过程中,每个结点都两次出现在栈顶,只有在第二次出现在栈顶时,才能访问它。因此需要多设置一个变量标识该结点是 否是第一次出现在栈顶。

test.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
 
#include <iostream>
#include <cstdio>
#include <stack>
#include <vector>
#include "BinaryTree.h"

using namespace std;

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */

//中介节点
struct TempNode
{
    TreeNode *btnode;
    bool isFirst;
};

//非递归后序遍历-迭代
vector<int> postTraversal(TreeNode *root)
{
    stack<TempNode *> s;
    vector<int> path;
    TreeNode *p = root;
    TempNode *temp;
    while(p != NULL || !s.empty())
    {
        while(p != NULL) //沿左子树一直往下搜索,直至出现没有左子树的结点
        {
            TempNode *tempNode = new TempNode;
            tempNode->btnode = p;
            tempNode->isFirst = true;
            s.push(tempNode);
            p = p->left;
        }
        if(!s.empty())
        {
            temp = s.top();
            s.pop();
            if(temp->isFirst == true)   //表示是第一次出现在栈顶
            {
                temp->isFirst = false;
                s.push(temp);
                p = temp->btnode->right;
            }
            else  //第二次出现在栈顶
            {
                path.push_back(temp->btnode->val);
                p = NULL;
            }
        }
    }
    return path;
}

// 树中结点含有分叉,
//                  8
//              /       \
//             6         1
//           /   \
//          9     2
//               / \
//              4   7
int main()
{
    TreeNode *pNodeA1 = CreateBinaryTreeNode(8);
    TreeNode *pNodeA2 = CreateBinaryTreeNode(6);
    TreeNode *pNodeA3 = CreateBinaryTreeNode(1);
    TreeNode *pNodeA4 = CreateBinaryTreeNode(9);
    TreeNode *pNodeA5 = CreateBinaryTreeNode(2);
    TreeNode *pNodeA6 = CreateBinaryTreeNode(4);
    TreeNode *pNodeA7 = CreateBinaryTreeNode(7);

ConnectTreeNodes(pNodeA1, pNodeA2, pNodeA3);
    ConnectTreeNodes(pNodeA2, pNodeA4, pNodeA5);
    ConnectTreeNodes(pNodeA5, pNodeA6, pNodeA7);

PrintTree(pNodeA1);

vector<int> ans = postTraversal(pNodeA1);

for (int i = 0; i < ans.size(); ++i)
    {
        cout << ans[i] << " ";
    }
    cout << endl;

DestroyTree(pNodeA1);
    return 0;
}

 
 
输出结果:
9 4 7 2 6 1 8 

【二叉树遍历模版】层次遍历

count 记录的是当前遍历的层次当中的结点个数。

depth记录的是当前遍历过的层次数。

test.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
 
#include <iostream>
#include <cstdio>
#include <stack>
#include <vector>
#include "BinaryTree.h"

using namespace std;

/**
 * Definition for binary tree
 * struct TreeNode {
 * int val;
 * TreeNode *left;
 * TreeNode *right;
 * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
vector<vector<int> > levelOrder(TreeNode *root)
{

vector<vector<int> > matrix;
    if(root == NULL)
    {
        return matrix;
    }
    vector<int> temp;
    temp.push_back(root->val);
    matrix.push_back(temp);

vector<TreeNode *> path;
    path.push_back(root);

int count = 1;
    while(!path.empty())
    {
        TreeNode *tn = path.front();
        if(tn->left)
        {
            path.push_back(tn->left);
        }
        if(tn->right)
        {
            path.push_back(tn->right);
        }
        path.erase(path.begin());
        count--;

if(count == 0)
        {
            vector<int> tmp;
            vector<TreeNode *>::iterator it = path.begin();
            for(; it != path.end(); ++it)
            {
                tmp.push_back((*it)->val);
            }
            if(tmp.size() > 0)
            {
                matrix.push_back(tmp);
            }
            count = path.size();
        }
    }
    return matrix;
}

// 树中结点含有分叉,
//                  8
//              /       \
//             6         1
//           /   \
//          9     2
//               / \
//              4   7
int main()
{
    TreeNode *pNodeA1 = CreateBinaryTreeNode(8);
    TreeNode *pNodeA2 = CreateBinaryTreeNode(6);
    TreeNode *pNodeA3 = CreateBinaryTreeNode(1);
    TreeNode *pNodeA4 = CreateBinaryTreeNode(9);
    TreeNode *pNodeA5 = CreateBinaryTreeNode(2);
    TreeNode *pNodeA6 = CreateBinaryTreeNode(4);
    TreeNode *pNodeA7 = CreateBinaryTreeNode(7);

ConnectTreeNodes(pNodeA1, pNodeA2, pNodeA3);
    ConnectTreeNodes(pNodeA2, pNodeA4, pNodeA5);
    ConnectTreeNodes(pNodeA5, pNodeA6, pNodeA7);

PrintTree(pNodeA1);

vector<vector<int> > ans = levelOrder(pNodeA1);

for (int i = 0; i < ans.size(); ++i)
    {
        for (int j = 0; j < ans[i].size(); ++j)
        {
            cout << ans[i][j] << " ";
        }
    }
    cout << endl;

DestroyTree(pNodeA1);
    return 0;
}

输出结果:
8 6 1 9 2 4 7
 
 

【二叉树遍历模版】Root->Right->Left遍历

先遍历根节点,然后在遍历这个根节点的右子树的根节点,然后递归对右子树做遍历,

遍历完事儿之后再递归遍历左子树。注意这种遍历方法很常用。本质上是一种DFS。

 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
 
#include <iostream>
#include <cstdio>
#include <stack>
#include <vector>
#include "BinaryTree.h"

using namespace std;

/**
 * Definition for binary tree
 * struct TreeNode {
 * int val;
 * TreeNode *left;
 * TreeNode *right;
 * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
vector<int> rootRLTraversal(TreeNode *root)
{
    stack<TreeNode *> s;
    vector<int> ans;
    TreeNode *tmp;
    s.push(root);

while (!s.empty())
    {
        tmp = s.top();
        s.pop();

if (!tmp)
        {
            continue;
        }
        //注意这里tmp不为空的时候才可以把他放进去
        ans.push_back(tmp->val);

s.push(tmp->left);
        s.push(tmp->right);
    }
    return ans;
}

// 树中结点含有分叉,
//                  6
//              /       \
//             7         2
//           /   \
//          1     4
//               / \
//              3   5
int main()
{
    TreeNode *pNodeA1 = CreateBinaryTreeNode(6);
    TreeNode *pNodeA2 = CreateBinaryTreeNode(7);
    TreeNode *pNodeA3 = CreateBinaryTreeNode(2);
    TreeNode *pNodeA4 = CreateBinaryTreeNode(1);
    TreeNode *pNodeA5 = CreateBinaryTreeNode(4);
    TreeNode *pNodeA6 = CreateBinaryTreeNode(3);
    TreeNode *pNodeA7 = CreateBinaryTreeNode(5);

ConnectTreeNodes(pNodeA1, pNodeA2, pNodeA3);
    ConnectTreeNodes(pNodeA2, pNodeA4, pNodeA5);
    ConnectTreeNodes(pNodeA5, pNodeA6, pNodeA7);

vector<int> ans = rootRLTraversal(pNodeA1);

for (int i = 0; i < ans.size(); ++i)
    {
        cout << ans[i] << " ";
    }
    cout << endl;

DestroyTree(pNodeA1);
    return 0;
}

 
结果输出:
6 2 7 4 5 3 1
 
BinaryTree.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 
#ifndef _BINARY_TREE_H_
#define _BINARY_TREE_H_

struct TreeNode
{
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

TreeNode *CreateBinaryTreeNode(int value);
void ConnectTreeNodes(TreeNode *pParent,
                      TreeNode *pLeft, TreeNode *pRight);
void PrintTreeNode(TreeNode *pNode);
void PrintTree(TreeNode *pRoot);
void DestroyTree(TreeNode *pRoot);

#endif /*_BINARY_TREE_H_*/

BinaryTree.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
 
#include <iostream>
#include <cstdio>
#include "BinaryTree.h"

using namespace std;

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */

//创建结点
TreeNode *CreateBinaryTreeNode(int value)
{
    TreeNode *pNode = new TreeNode(value);

return pNode;
}

//连接结点
void ConnectTreeNodes(TreeNode *pParent, TreeNode *pLeft, TreeNode *pRight)
{
    if(pParent != NULL)
    {
        pParent->left = pLeft;
        pParent->right = pRight;
    }
}

//打印节点内容以及左右子结点内容
void PrintTreeNode(TreeNode *pNode)
{
    if(pNode != NULL)
    {
        printf("value of this node is: %d\n", pNode->val);

if(pNode->left != NULL)
            printf("value of its left child is: %d.\n", pNode->left->val);
        else
            printf("left child is null.\n");

if(pNode->right != NULL)
            printf("value of its right child is: %d.\n", pNode->right->val);
        else
            printf("right child is null.\n");
    }
    else
    {
        printf("this node is null.\n");
    }

printf("\n");
}

//前序遍历递归方法打印结点内容
void PrintTree(TreeNode *pRoot)
{
    PrintTreeNode(pRoot);

if(pRoot != NULL)
    {
        if(pRoot->left != NULL)
            PrintTree(pRoot->left);

if(pRoot->right != NULL)
            PrintTree(pRoot->right);
    }
}

void DestroyTree(TreeNode *pRoot)
{
    if(pRoot != NULL)
    {
        TreeNode *pLeft = pRoot->left;
        TreeNode *pRight = pRoot->right;

delete pRoot;
        pRoot = NULL;

DestroyTree(pLeft);
        DestroyTree(pRight);
    }
}

 
     

【二叉树遍历模版】前序遍历&&中序遍历&&后序遍历&&层次遍历&&Root->Right->Left遍历的更多相关文章

  1. Java实现二叉树的前序、中序、后序遍历(非递归方法)

      在上一篇博客中,实现了Java中二叉树的三种遍历方式的递归实现,接下来,在此实现Java中非递归实现二叉树的前序.中序.后序遍历,在非递归实现中,借助了栈来帮助实现遍历.前序和中序比较类似,也简单 ...

  2. LeetCode二叉树的前序、中序、后序遍历(递归实现)

    本文用递归算法实现二叉树的前序.中序和后序遍历,提供Java版的基本模板,在模板上稍作修改,即可解决LeetCode144. Binary Tree Preorder Traversal(二叉树前序遍 ...

  3. Java实现二叉树的前序、中序、后序、层序遍历(非递归方法)

      在上一篇博客中,实现了Java中二叉树的四种遍历方式的递归实现,接下来,在此实现Java中非递归实现二叉树的前序.中序.后序.层序遍历,在非递归实现中,借助了栈来帮助实现遍历.前序和中序比较类似, ...

  4. 【2】【leetcode-105,106】 从前序与中序遍历序列构造二叉树,从中序与后序遍历序列构造二叉树

    105. 从前序与中序遍历序列构造二叉树 (没思路,典型记住思路好做) 根据一棵树的前序遍历与中序遍历构造二叉树. 注意:你可以假设树中没有重复的元素. 例如,给出 前序遍历 preorder = [ ...

  5. PHP递归方法实现前序、中序、后序遍历二叉树

    二叉树是每个节点最多有两个子树的树结构.通常子树被称作“左子树”(left subtree)和“右子树”(right subtree). class Node { public $value; pub ...

  6. leetcode 105 106 从前序与中序遍历序列构造二叉树 从中序与后序遍历序列构造二叉树

    题目: 105 根据一棵树的前序遍历与中序遍历构造二叉树. 注意:你可以假设树中没有重复的元素. 例如,给出 前序遍历 preorder = [3,9,20,15,7] 中序遍历 inorder = ...

  7. 二叉树各种相关操作(建立二叉树、前序、中序、后序、求二叉树的深度、查找二叉树节点,层次遍历二叉树等)(C语言版)

    将二叉树相关的操作集中在一个实例里,有助于理解有关二叉树的相关操作: 1.定义树的结构体: typedef struct TreeNode{ int data; struct TreeNode *le ...

  8. Python实现二叉树的前序、中序、后序、层次遍历

      有关树的理论部分描述:<数据结构与算法>-4-树与二叉树:   下面代码均基于python实现,包含: 二叉树的前序.中序.后序遍历的递归算法和非递归算法: 层次遍历: 由前序序列.中 ...

  9. 数据结构-二叉树(1)以及前序、中序、后序遍历(python实现)

    上篇文章我们介绍了树的概念,今天我们来介绍一种特殊的树--二叉树,二叉树的应用很广,有很多特性.今天我们一一来为大家介绍. 二叉树 顾名思义,二叉树就是只有两个节点的树,两个节点分别为左节点和右节点, ...

  10. 二叉树的查找(前序、中序、后序、层序遍历)--biaobiao88

    建立一棵含有n个结点的二叉树,采用二叉链表存储: 输出前序.中序.后序..层序遍历该二叉树的遍历结果. 定义二叉树的数据类型——二叉树结点结构体BiNode.建立二叉链表可以采用扩展二叉树的一个遍历序 ...

随机推荐

  1. Shell指令

    Shell指令 1.Shell原理图 2.Shell指令的基本语法 Shell指令 Shell –选项 参数 Shell –选项 Shell参数 3.常用的Shell指令 1)ls指令:显示文件信息 ...

  2. 杭电2034——人见人爱A-B

    #include <stdio.h> #include <algorithm> using namespace std; int main () { int a[110],b[ ...

  3. 《Apache负载均衡》RHEL6

    Apache负载均衡的搭建,基于上篇<CDN web加速代理>搭建好的服务器,我们来搭建apache负载均衡: Apahe负载均衡:就是为了缓解一台服务器的压力而多台服务器配合使用. 基于 ...

  4. WInform启动另一个项目传值

    背景:从A项目中登陆后,跳转到B项目的某个页面(B不再登陆). A项目启动进程: public Form1() { InitializeComponent(); } #region 调用进程 [Dll ...

  5. Mysql支持中文全文检索的插件mysqlcft-应用中的问题

    MySQL目前版本的全文检索没有对中文很好的支持,但可以通过安装mysqlcft插件来实现,具体的安装使用方法:http://blog.s135.com/post/356/ mysqlcft的官方网站 ...

  6. Noppoo choc mini 84 @XUbuntu13.10 compatibility setting

    Months ago, I bought the keyboard Noppoo Choc Mini 84keys for using under XUbuntu12.10, and I have f ...

  7. 【转】RunTime.getRunTime().addShutdownHook用法

    Runtime.getRuntime().addShutdownHook(shutdownHook); 这个方法的含义说明: 这个方法的意思就是在jvm中增加一个关闭的钩子,当jvm关闭的时候,会执行 ...

  8. WPF与DevExpress之旅-序言

    随着.NET技术的发展,从之前的WINFORM转向到WPF是我们技术改革的必然趋势.WPF能给人带来震撼的视觉体验,也能更加规范我们的开发模式,与传统的WINFORM开发来说具有革命性的意义.DevE ...

  9. 【js】 流式布局 页面

    <!DOCTYPE html><html><head> <meta content="text/html; charset=utf-8" ...

  10. React和Backbone优缺点

    1.React 使用了VDOM,方便移植至其他平台,如Android等:Backbone更灵活,且与Jquery结合比较好. 2.React如果不与Jsx结合易读性很差;Backbone有强大的模板功 ...