//先序遍历
#include
using namespace std;
struct TreeNode
{
int val;
TreeNode *left, *right;
};
//先序遍历
void PreorderTraversal(TreeNode* root) {
if (!root) return;
cout << root->val << endl;
if (root->left)
PreorderTraversal(root->left);
if (root->right)
PreorderTraversal(root->right);
}
//中序遍历
void InorderTraversal (TreeNode* root){
if (!root) return;
if (root->left)
InorderTraversal(root->left);
cout << root->val << endl;
if (root->right)
InorderTraversal(root->right);
}
//后序遍历
void PostorderTraversal (TreeNode* root){
if (!root) return;
if (root->left)
InorderTraversal(root->left);
if (root->right)
InorderTraversal(root->right);
cout << root->val << endl;
}
int main()
{
...
}