每日一题 -- 树
1026. 节点与其祖先之间的最大差值
1026. 节点与其祖先之间的最大差值
var maxAncestorDiff = function(root) {
if(!root) return 0
let res = -Infinity
const dfs = (root,min,max) => {
min = Math.min(root.val,min)
max = Math.max(root.val,max)
if(!root.left && !root.right){
res = Math.max(res,max-min)
return
}
if(root.left) dfs(root.left,min,max)
if(root.right) dfs(root.right,min,max)
}
dfs(root,Infinity,-Infinity)
return res
};
91算法 -- 搜索
301. 删除无效的括号
301. 删除无效的括号
分析
- 先把所有执行情况都弄出来,然后写边界,最后进行优化
- 首先遍历 s 的每一个字符需要判定的执行的情况有4种
- 左括号 -- 存储 或者 删除
- 右括号且左括号存储数存在 -- 抵消 或者 删除
- 右括号且左括号存储数为0 -- 删除
- 其他字符 -- 跳过
- 边界
- 保存一个全局最小的删除数 deleteCount,当 del 的值大于 deleteCount 的时候,返回;注意,由于需要设置真正的初始最小值,所以在遇到右括号的时候,先进行抵消操作,这样首次满足条件返回字符串的时候, del 其实就是最小的删除数,后续的值都以它为标准即可
- 当遍历 s 结束的时候,如果 leftCount 也清空了,则这条路径是好的;但是需要注意的是,很有可能存在不同路径得到相同的字符串的情况,所以还需要数组去重
回调函数的参数
- subStr -- 遍历过程中保存的字符串
- leftCount -- 左括号存储数
- index -- 遍历的下标,作为 s 遍历结束的终止条件和查找 s 字符
- del -- 表示当前路径已经删除了字符的值
var removeInvalidParentheses = function (s) {
const res =[]
let deleteCount = Infinity;
const recursion = (subStr, leftCount, index, del) => {
if (del > deleteCount) return
if(s.length === index){
if(leftCount === 0 && res.indexOf(subStr) === -1){
res.push(subStr)
deleteCount = Math.min(del,deleteCount)
return
}else{
return
}
}
if (s[index] === '(') {
recursion(subStr + s[index], leftCount + 1, index + 1, del)
recursion(subStr, leftCount, index + 1, del + 1)
}
if (s[index] === ')' && leftCount) {
recursion(subStr + s[index], leftCount - 1, index + 1, del)
recursion(subStr, leftCount, index + 1, del + 1)
}
if(s[index] === ')' && leftCount === 0){
recursion(subStr, leftCount, index + 1, del + 1)
}
if(s[index]!=='(' && s[index]!==')') {
recursion(subStr + s[index], leftCount, index + 1, del)
}
}
recursion('',0,0,0)
return res
};