数据结构算法
二叉树的遍历
//先序遍历
void preorder(TreeNode * root)
{
if root==null
return;
cout<<root.val<<endl;
preorder(root.left);
preorder(root.right);
}
//中序遍历
void inorder(TreeNode *root)
{
if root==null
return;
inorder(root.left);
cout<<root.val;
inorder(root.right);
}
//后序遍历
void postoder(TreeNode *root)
{
if root==null
return;
inorder(root.left);
inorder(root.right);
cout<<root.val;
}