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)
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {number[]}
*/
var inorderTraversal = function(root) {
function traverse(node, rst) {
if(node.left) {
traverse(node.left, rst);
}
rst.push(node.val);
if(node.right) {
traverse(node.right, rst);
}
}
const rst = [];
if(!root) {
return rst;
} else {
traverse(root, rst);
return rst;
}
};