二叉树的遍历

2020-10-13  本文已影响0人  程序猿Cyinen

数据结构算法

二叉树的遍历


//先序遍历

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;

}

上一篇 下一篇

猜你喜欢

热点阅读