一、手写算法
https://leetcode-cn.com/problems/find-bottom-left-tree-value/
思路
-
代码
```javascript /**
- 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 findBottomLeftValue = function(root) { var maxD = -1,valB = 0;
var find = function (root,curD) {
if(root===null) return null;
if(maxD < curD){
maxD = curD;
valB = root.val;
}
find(root.left,curD+1)
find(root.right,curD+1)
};
find(root,0); return valB
};
<a name="fbzED"></a>
### 复杂度分析
- 时间复杂度:
- 空间复杂度:
<a name="Ghcnm"></a>
## 二、编程题
```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
class Man {
constructor(name) {
this.actions = [];
this.name = name;
const sayHello = () => {
console.log('Hi! This is' + name);
this.next();
};
this.addAction(sayHello);
setTimeout(() => {
this.next();
}, 0);
}
next() {
if (this.actions.length > 0) {
const fn = this.actions.shift();
if ((typeof fn).toLowerCase() === 'function') {
fn();
}
}
}
addAction(func, isFirst) {
if (!isFirst) {
this.actions.push(func);
} else {
this.actions.unshift(func);
}
}
sleep(timeout) {
const that = this;
const sleepFunc = setTimeout(() => {
console.log(`Wake up after ${timeout}`);
this.next();
}, timeout);
this.addAction(sleepFunc);
return this;
}
eat(food) {
const eatFunc = () => {
console.log(`Eat ${food}~`);
this.next();
};
this.addAction(eatFunc);
return this;
}
sleepFirst(timeout) {
const sleepFunc = setTimeout(() => {
console.log(`Wake up after ${timeout}`);
this.next();
}, timeout);
this.addAction(sleepFunc,true);
return this;
}
}
function LazyMan(name) {
return new Man(name);
}
LazyMan('Hank');
LazyMan('Hank').sleep(1000).eat('dinner');
LazyMan('Hank').eat('dinner').eat('supper');
LazyMan('Hank').eat('supper').sleepFirst(1000);