#includeusing namespace std; //树节点定义 struct Node { int val; Node *lchild; Node *rchild; Node(int x) : val(x), lchild(), rchild() {} }; //先序遍历 void preOrder(Node *root) { if (root == nullptr) return ; cout << root->val << endl; preOrder(root->lchild); preOrder(root->rchild); } //中序遍历 void inOrder(Node *root) { if (root == nullptr) return ; inOrder(root->lchild); cout << root->val << endl; inOrder(root->rchild); } //后序遍历 void postOrder(Node *root) { if (root == nullptr) return ; postOrder(root->lchild); postOrder(root->rchild); cout << root->val << endl; } int main() { Node *root = new Node(1); root->lchild = new Node(2); root->rchild = new Node(3); root->lchild->lchild = new Node(4); root->lchild->rchild = new Node(5); root->rchild->lchild = new Node(6); root->rchild->rchild = new Node(7); preOrder(root); cout << "==============" << endl; inOrder(root); cout << "==============" << endl; postOrder(root); cout << "==============" << endl; return 0; }



