Binary Tree Inorder Traversal
Description
Given a binary tree, return the inorder traversal of its nodes' values.
Example:
Input: [1,null,2,3] 1 \ 2 / 3Output: [1,3,2]
Follow up: Recursive solution is trivial, could you do it iteratively?
Solution(javascript)
/*
* @lc app=leetcode id=94 lang=javascript
*
* [94] Binary Tree Inorder Traversal
*/
// @lc code=start
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {number[]}
*/
const inorderTraversal = function (root) {
const stack = []
const result = []
const push = (node) => {
while (node) {
stack.push(node)
node = node.left
}
}
const pop = () => {
while (stack.length) {
const node = stack.pop()
result.push(node.val)
push(node.right)
}
}
push(root)
pop()
return result
}