Skip to content

Instantly share code, notes, and snippets.

@RP-3
Created July 2, 2020 07:54
Show Gist options
  • Save RP-3/e44e1885f080ca581a0c0a2f7d97f0d9 to your computer and use it in GitHub Desktop.
Save RP-3/e44e1885f080ca581a0c0a2f7d97f0d9 to your computer and use it in GitHub Desktop.
/**
* 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) {
const result = [];
const pot = (node, depth) => {
if(!node) return;
if(!result[depth]) result[depth] = [];
result[depth].push(node.val);
pot(node.left, depth+1);
pot(node.right, depth+1);
};
pot(root, 0);
return result.reverse();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment