1. 前、中、后是指根节点的位置
    2. 中序遍历 =》
    3. /**
    4. * Definition for a binary tree node.
    5. * function TreeNode(val, left, right) {
    6. * this.val = (val===undefined ? 0 : val)
    7. * this.left = (left===undefined ? null : left)
    8. * this.right = (right===undefined ? null : right)
    9. * }
    10. */
    11. /**
    12. * @param {TreeNode} root
    13. * @return {number[]}
    14. */
    15. var inorderTraversal = function (root) {
    16. const list = []
    17. helper(root, list)
    18. return list
    19. };
    20. const helper = (node, list) => {
    21. if(!node) return
    22. helper(node.left, list)
    23. list.push(node.val)
    24. helper(node.right, list)
    25. }
    1. var inorderTraversal = function (root) {
    2. const stack = [],
    3. list = []
    4. let node = root
    5. while (node || stack.length) {
    6. if (node) {
    7. stack.push(node)
    8. node = node.left
    9. } else {
    10. node = stack.pop()
    11. list.push(node.val)
    12. node = node.right
    13. }
    14. }
    15. return list
    16. };