二叉树的遍历——DFS

在这里插入图片描述

先序遍历

void preorder(node* root)
{
    
    
	if(root == NULL) return;
	printf("%d\n", root->data);
	preorder(root->lchild);
	preorder(root->rchild);
}

中序遍历

void inorder(node* root)
{
    
    
	if(root == NULL) return;
	inorder(root->lchild);
	printf("%d\n", root->data);
	inorder(root->rchild);
}

后序遍历

void postorder(node* root)
{
    
    
	if(root == NULL) return;
	postorder(root->lchild);
	postorder(root->rchild);
	printf("%d\n", root->data);
}

猜你喜欢

转载自blog.csdn.net/tian__si/article/details/113969604