树的遍历本质上是一种递归 树的遍历,前序中序后序,前中后的意思就是根节点的前中后的位置
数据结构:
前序: preOrder(root) { if(root) { console.log(root.val); preOrder(root.left); preOrder(root.right); } }
中序: inOrder(root) { if(root) { inOrder(root.left); console.log(root.val); inOrder(root.right); } }
后序: postOrder(root) { if(root) { postOrder(root.left); postOrder(root.right); console.log(root.val); } }