二叉树遍历
二叉树节点的定义
1 | function TreeNode(val) { |
先序遍历
根节点 -> 左子树 -> 右子树
递归
1 | function preOrder(root, result = []) { |
非递归
1 | function preOrder(root) { |
中序遍历
左子树 -> 根节点 -> 右子树
递归
1 | function inOrder(root, result = []) { |
非递归
1 | function inOrder(root) { |
后序遍历
左子树 -> 右子树 -> 根节点
递归
1 | function postOrder(root, result = []) { |
非递归
1 | function postOrder(root) { |