Back to all solutions
#107 - Binary Tree Level Order Traversal II
Problem Description
Given the root of a binary tree, return the bottom-up level order traversal of its nodes' values.
(i.e., from left to right, level by level from leaf to root).
Solution
/**
* 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 levelOrderBottom = function(root) {
if (!root) return [];
const result = [];
const stack = [root];
while (stack.length > 0) {
const level = [];
const n = stack.length;
for (let i = 0; i < n; i++) {
const node = stack.shift();
level.push(node.val);
if (node.left) stack.push(node.left);
if (node.right) stack.push(node.right);
}
result.unshift(level);
}
return result;
};