给定二叉树的根节点,返回该二叉树节点值的中序遍历结果。

限制条件:

  • 树节点的数量范围是 [0, 100].
  • -100 <= Node.val <= 100

解法:

/**
 * 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) {
    
    let curr = root; // currnet node
    const stack = [];
    const res = [];

    while(curr || stack.length) {
        
        while(curr) {
            stack.push(curr);
            curr = curr.left;
        }
        
        curr = stack.pop();
        if (curr) {
            res.push(curr.val);
        }
        
        curr = curr.right;
    }
    
    return res;
};