美团前端二面面经
发面经,攒人品。。(二面面试官很有耐心,引导的很好。。点赞)
1. 二叉树的中序遍历实现
// Way1. 栈结构实现 function inOrder(root) { var res = []; var stack = []; var p = root; while (p || stack.length > 0) { while (p) { stack.push(p); p = p.left; } p = stack.pop(); res.push(p.node); p = p.right } return res } // Way2. 递归实现 function inOrderCore(root, res) { if (!root) { return } inOrderCore(root.left, res); res.push(root.node); inOrderCore(root.right, res); } function inOrder2(root) { var res = []; inOrderCore(root, res); return res; }
3. 二叉树的节点为N,那么二叉树的深度为多少,怎么推导的。。(logN)
4. 算法实现二叉树深度
function dfs(root) { if (!root) { return 0 }else { var leftDepth = dfs(root.left); var rightDepth = dfs(root.right); var childDepth = leftDepth > rightDepth ? leftDepth : rightDepth; return childDepth + 1 } }
5. 闭包和JS垃圾回收机制
6. 利用闭包实现按列升降序排序函数(0表示升序,1表示降序)
function listSort(testArr, str) { return function (sortWay) { // 比较 if (sortWay) { testArr.sort((a, b) => {return b[str] - a[str]}) }else { testArr.sort((a, b) => {return a[str] - b[str]}) } return testArr } } var testArray = [{name: 1,age: 18,mobile: 123},{name: 2,age: 19,mobile: 234},{name: 3,age: 20,mobile: 456}]; console.log(listSort(testArray, "name")(1));
8. Generator的\*表示什么意思,如果在yield后加\*号表示什么
9. 介绍一下BFC