每日一题 -- 树
求和路径
求和路径
var pathSum = function(root, sum) {
const res = []
const innerDfs = (root,rest,arr) => {
const temp = rest-root.val
if(temp === 0) {
res.push([...arr,root.val])
}
if(!root.left && !root.right) return
if(root.left) innerDfs(root.left,temp,[...arr,root.val])
if(root.right) innerDfs(root.right,temp,[...arr,root.val])
}
const outDfs = (root) => {
if(!root) return
innerDfs(root,sum,[])
outDfs(root.left)
outDfs(root.right)
}
outDfs(root)
return res.length
};
var pathSum = function(root, sum) {
const total = 0
const innerDfs = (root,rest) => {
const temp = rest-root.val
if(temp === 0) {
total++
}
if(!root.left && !root.right) return
if(root.left) innerDfs(root.left,temp)
if(root.right) innerDfs(root.right,temp)
}
const outDfs = (root) => {
if(!root) return
innerDfs(root,sum)
outDfs(root.left)
outDfs(root.right)
}
outDfs(root)
return res.length
};
91算法 -- 位运算
0~n-1中缺失的数字
0~n-1中缺失的数字
var missingNumber = function(nums) {
let res= nums.length
nums.forEach((item,index) => {
res^=item^index
})
return res
};