前、中、后是指根节点的位置 中序遍历 =》 左 中 右/** * Definition for a binary tree node. * function TreeNode(val, left, right) { * this.val = (val===undefined ? 0 : val) * this.left = (left===undefined ? null : left) * this.right = (right===undefined ? null : right) * } *//** * @param {TreeNode} root * @return {number[]} */var inorderTraversal = function (root) { const list = [] helper(root, list) return list};const helper = (node, list) => { if(!node) return helper(node.left, list) list.push(node.val) helper(node.right, list)}
var inorderTraversal = function (root) { const stack = [], list = [] let node = root while (node || stack.length) { if (node) { stack.push(node) node = node.left } else { node = stack.pop() list.push(node.val) node = node.right } } return list};