Last active
July 17, 2021 11:35
-
-
Save ravitejamuddada/7cc6ff6de559695df2f8a6f867d326b1 to your computer and use it in GitHub Desktop.
Leetcode Binary Tree Level Order Traversal
This file contains 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 levelOrder = function(root) { | |
let res=[]; | |
let level=0; | |
const LT=(root,level)=>{ | |
if(!root) return; | |
if(res[level]){ | |
res[level].push(root.val) | |
} | |
else{ | |
res[level]=[root.val] | |
} | |
LT(root.left,level+1) | |
LT(root.right,level+1) | |
} | |
LT(root,0); | |
/* | |
//for bottom up | |
res.reverse() | |
*/ | |
return res; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment