二叉树的遍历
二叉树有三种典型的遍历方法
1. 先序遍历,即先根遍历,根-左子树-右子树
先序遍历有点类似DFS算法的过程,尤其于DFS的迭代写法很像,后面会介绍DFS的相关内容。
preorder(Tree T) if T is not null visit(T) preorder(T.left) preorder(T.right)
2. 中序遍历,即中根遍历,左子树-根-右子树
inorder(TREE T) if T is not null inorder(T.left) visit(T) inorder(T.right)
3. 后序遍历,即后根遍历,左子树-右子树-根
postorder(TREE T) if T is not null postorder(T.left) postorder(T.right) visit(T)