算法题
- 链接:https://leetcode-cn.com/problems/find-bottom-left-tree-value/submissions/
- 题目:
- 给定一个二叉树的 根节点 root,请找出该二叉树的 最底层 最左边 节点的值。
- 假设二叉树中至少有一个节点。
解题
var findBottomLeftValue = function(root) {let maxLen = 0;let res = null;const getLeftNode=function(node,curLen) {if (!node.left && !node.right) {if (curLen>maxLen) {maxLen = curLenres = node.val}}node.left && getLeftNode(node.left,curLen+1)node.right && getLeftNode(node.right,curLen+1)}getLeftNode(root ,1)return res};
知道用递推,一写就废,自己写的很拧巴,待遇加强递归的终结条件
手写题
题目 ```javascript 实现一个LazyMan,可以按照以下方式调用: LazyMan(“Hank”)输出: Hi! This is Hank!
LazyMan(“Hank”).sleep(10).eat(“dinner”)输出 Hi! This is Hank! //等待10秒.. Wake up after 10 Eat dinner~
LazyMan(“Hank”).eat(“dinner”).eat(“supper”)输出 Hi This is Hank! Eat dinner~ Eat supper~ LazyMan(“Hank”).eat(“supper”).sleepFirst(5)输出 //等待5秒 Wake up after 5 Hi This is Hank! Eat supper
- 解题```javascriptfunction delay(time) {return new Promise(resolve => {setTimeout(() => {resolve()}, time);})}function LazyMan(value) {const tasks = [{type: 'immediately',value: ` Hi! This is ${value}!`}]let actions = {eat(food) {tasks.push({type: 'immediately',text: ` Eat ${food}!`,value:food})return this},sleep(time) {tasks.push({type: 'delay',text: ` Wake up after ${time}!`,value:time})return this},sleepFirst(time) {tasks.unshift({type: 'delay',text: ` Wake up after ${time}!`,value:time})return this}}async function execFn (){for (let item of tasks){if (item.type ==='delay') {await delay(item.value)console.log(item.text)} else {console.log(item.value)}}}setTimeout(execFn);return actions}LazyMan("Hank").eat("dinner").sleepFirst(1000);
