一、二叉树层级遍历
// 初始化二叉树
function TreeNode(val){
this.val = val;
this.left = this.right =null;
}
function levelOrder(root) {
var res = [];
if(!root) return res;
recusion(root,0);
return res;
function recusion(root,level) {
if(!root) return;
// 判断当前level层是否已存在元素
if(res[level]){
res[level].push(root.val);
} else {
res[level] = [root.val];
}
if(root.left){
recusion(root.left,level+1);
}
if(root.right){
recusion(root.right,level+1);
}
}
}
二、求二叉树最大深度
// 初始化二叉树
function TreeNode(val){
this.val = val;
this.left = this.right =n