Created
July 2, 2020 07:54
-
-
Save RP-3/e44e1885f080ca581a0c0a2f7d97f0d9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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