经常碰到笔试题问这个,故总结一下
- 前序遍历(preorder traversal)
访问顺序: Root, Left, Right
Preorder traversal is used to create a copy of the tree. Preorder traversal is also used to get prefix expression on of an expression tree
Why prefix is useful?
polish notation
2. 中序遍历 (inorder traversal)
访问顺序: Left, Root, Right
Inorder Traversal gives the value from small to large when used on a BST
3. 后序遍历 (postorder traversal)
访问顺序: Left,Right, Root
Postorder traversal is used to delete the tree
delete tree
Postorder traversal is also useful to get the postfix expression of an expression tree
postfix expression
几种遍历的转化