如何轻松难忘地记住中序遍历和前序遍历?

How to remember in-order traversal and pre-order traversal in an easy and unforgettable way?

前序遍历、中序遍历、后序遍历、层序遍历等二叉树遍历,很多IT公司都会面试。

前序遍历和中序遍历的迭代实现让我记忆犹新

这里是leetcode的题目。

https://leetcode.com/problems/binary-tree-inorder-traversal/

https://leetcode.com/problems/binary-tree-preorder-traversal/

  1. 前序遍历:

We walk the graph, from top going counter-clockwise. Shout every time we pass the LEFT of a node.

  1. 中序遍历:

We walk the graph, from top going counter-clockwise. Shout every time Shout when you cross the bottom.

  1. post-顺序遍历

We walk the graph, from top going counter-clockwise. Shout every time Shout when you cross the right

如果您想查看更多递归和迭代的实现细节,请阅读following post

我记得是参考根节点想的

Inorder -> 表示根是 InBetween Left and Right,

PreOrder -> Root为'pre'/before Left and right

Post -> Root 在 Left 和 Right 之后。