二叉树: 二叉树的前序遍历

55 阅读1分钟

方法:

  • 前序:根左右
  • 中序:左根右
  • 后续:左右根
function preorderTraversal( root ) {
    const result = []; // 存储遍历结果
    const preOrder = (root) => {
        if(!root) return;
        result.push(root.val); // 根
        preOrder(root.left); // 左
        preOrder(root.right); // 右
    }
    preOrder(root);
    return result;
}