数据结构 -- 二叉树 遍历
二叉树遍历方式
深度优先遍历(DFS)
- 前序遍历 Pre-order Traversal
跟–左—右顺序遍历
We start from A, and following pre-order traversal, we first visit A itself and then move to its left subtree B. B is also traversed pre-order. The process goes on until all the nodes are visited. The output of pre-order traversal of this tree will be −
A → B → D → E → C → F → G
- 中序遍历 In-order Traversal
左-- 根 — 右
We start from A, and following in-order traversal, we move to its left subtree B. B is also traversed in-order. The process goes on until all the nodes are visited. The output of inorder traversal of this tree will be −
D → B → E → A → F → C → G
- 后续遍历
左-- 右-- 根
We start from A, and following pre-order traversal, we first visit A itself and then move to its left subtree B. B is also traversed pre-order. The process goes on until all the nodes are visited. The output of pre-order traversal of this tree will be −
A → B → D → E → C → F → G
总结:根据遍历顺序,不断的细分左右子树
广度优先遍历(BFS) 即层次遍历
从左到右,从上下到下遍历
二、实现
添加要改颜色的字体
添加要改颜色的字体
添加要改颜色的字体
添加要改颜色的字体