难度: 简单
时间复杂度 O(n)
/**
* 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 result = [];
function loop (node) {
if (!node) return
const left = node.left;
const right = node.right;
left && loop(left);
result.push(node.val);
right && loop(right);
}
loop(root);
return result;
};
/**
* 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 result = [];
const stack = [];
while (root) {
stack.push(root);
root = root.left;
}
while (stack.length) {
let target = stack.pop();
result.push(target.val);
target = target.right;
while (target) {
stack.push(target);
target = target.left;
}
}
return result;
};