二叉树遍历-递归
- 前序遍历(Pre-Order)
- 中序遍历(In-Order)
- 后序遍历(Post-Order)
前序遍历
//c++:前序遍历
void preOrder(TreeNode *root){
if (root == NULL) return;
cout<<root->val<<endl;
preOrder(root->left);
preOrder(root->right);
}
中序遍历
//c++:中序遍历
void inOrder(TreeNode *root){
if (root == NULL) return;
inOrder(root->left);
cout<<root->val<<endl;
inOrder(root->right);
}
后序遍历
//c++:后序遍历
void postOrder(TreeNode *root){
if (root == NULL) return;
postOrder(root->left);
postOrder(root->right);
cout<<root->val<<endl;
}