## 二叉树遍历 (先序遍历、中序遍历、后序遍历) 递归 原

老汉-憨憨

``````#include <iostream>
using namespace std;

// binary tree node
struct Node
{
int data;
Node *left,*right;
Node(int x)
{
data = x;
left = right = NULL;
}
};

void PreOrderVisitTree(Node *root)
{
// empty node
if (!root)
return;

cout<<(root->data)<< "\t";
PreOrderVisitTree(root->left);
PreOrderVisitTree(root->right);
}

void InOrderVisitTree(Node *root)
{
// empty node
if (!root)
return;

InOrderVisitTree(root->left);
cout<<(root->data)<< "\t";
InOrderVisitTree(root->right);
}

void PostorderVisitTree(Node *root)
{
// empty node
if (!root)
return;
PostorderVisitTree(root->left);
PostorderVisitTree(root->right);
cout <<(root->data)<< "\t";
}

// Driver code
int main()
{
Node *root = new Node(1);
root->left = new Node(3);
root->left->left = new Node(2);
root->left->right = new Node(1);
root->left->right->left = new Node(1);
root->right = new Node(-1);
root->right->left = new Node(4);
root->right->left->left = new Node(1);
root->right->left->right = new Node(2);
root->right->right = new Node(5);
root->right->right->right = new Node(2);

/*前序遍历*/
cout << "NLR:\t";
PreOrderVisitTree(root);
cout << endl;

/*中序遍历*/
cout << "LNR:\t";
InOrderVisitTree(root);
cout << endl;

/*后序遍历*/
cout << "LRN:\t";
PostorderVisitTree(root);
cout << endl;

return 0;
}
``````

``````NLR:	1	3	2	1	1	-1	4	1	2	5	2
LNR:	2	3	1	1	1	1	4	2	-1	5	2
LRN:	2	1	1	3	1	2	4	2	5	-1	1	``````

### 老汉-憨憨

xiaoxiaoxuanao
2017/03/27
0
0
python实现二叉树的遍历以及基本操作

pandaWaKaKa
06/25
0
0

07/21
0
0

qq_38646470
2018/01/21
0
0

datacube
2016/07/07
215
0

1分钟前
0
0
Java Timer的用法

Timer timer = new Timer(); timer.schedule(new TimerTask() { public void run() { System.out.println("11232"); } }, 200000 , 1000); public void schedule(TimerTask task, long delay......

5分钟前
2
0

Bing309
12分钟前
2
0

16分钟前
2
0

16分钟前
2
0