var rightSideView = function(root) {
if(!root){
return []
}
let res=[]
let queue=[root]
while(queue.length){
let l=queue.length
let cur=[]
for(let i=0;i<l;i++){
let node=queue.shift()
cur.push(node.val)
if(node.left){
queue.push(node.left)
}
if(node.right){
queue.push(node.right)
}
}
res.push(cur[cur.length-1])
}
return res
};
var pruneTree = function(root) {
if(!root){
return root
}
root.left=pruneTree(root.left)
root.right=pruneTree(root.right)
return (root.left || root.right || root.val===1)?root:null
}
const dfs=(root,pre)=>{
if(!root){
return 0
}
let sum=pre*10+root.val
if(!root.left && !root.right){
return sum
}else{
return dfs(root.left,sum)+dfs(root.right,sum)
}
}
var sumNumbers = function(root) {
return dfs(root,0)
};
var pathSum = function(root, targetSum) {
if(!root){
return 0
}
let res=rootSum(root,targetSum)
res+=pathSum(root.left,targetSum)
res+=pathSum(root.right,targetSum)
return res
};
const rootSum=(root,targetSum)=>{
if(!root){
return 0
}
let res=0
let val=root.val
if(val===targetSum){
res++
}
res+=rootSum(root.left,targetSum-val)
res+=rootSum(root.right,targetSum-val)
return res
}