一、手写算法
https://leetcode-cn.com/problems/sum-root-to-leaf-numbers/
思路
代码
```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}
*/
const dfs = (root, prevSum) => {
if (root === null) {
} const sum = prevSum * 10 + root.val; if (root.left == null && root.right == null) {return 0;
} else {return sum;
} }return dfs(root.left, sum) + dfs(root.right, sum);
var sumNumbers = function(root) { return dfs(root, 0); };
<a name="fbzED"></a>
### 复杂度分析
- 时间复杂度:
- 空间复杂度:
<a name="Ghcnm"></a>
## 二、编程题
```javascript
// https://bigfrontend.dev/zh/problem/remove-characters
/**
* 解法1
* @param {string} input
* @returns string
*/
function removeChars(input) {
// your code here
var len = input.length;
var stack = [];
var i = 0;
while (i < len) {
if (input[i] === 'c') {
if (stack.length > 0) {
var temp = stack[stack.length - 1];
temp === 'a' ? stack.pop() : stack.push(input[i]);
} else {
stack.push(input[i]);
}
}
if (input[i] === 'a') {
stack.push(input[i]);
}
i++;
}
return stack.join('');
}
/**
* 解法2
* @param {string} input
* @returns string
*/
function removeChars(input) {
const regExp = /b|(ac)/g;
while (input.match(regExp)) {
input = input.replace(regExp, '');
}
return input;
}