每天刷算法题 要加油呦!自勉😊
这类问题大部分做法都是递归处理左右子树,获取我们想要的结果
eg: 235-二叉搜索树的公共祖先
eg: 700-二叉搜索树的搜索
不同之处就在于 是先遍历再往数组内存,还是先存后遍历
这道题就是带着深度值进行深度优先遍历,
var maxDepth = function(root) {
const dfs = (root,depth)=> {
if(!root) return 0;
depth++
let temp = depth
for(let item of root.children) {
depth = Math.max(depth,dfs(item,temp))
}
return depth
}
return dfs(root,0)
};
求二叉树路径相关问题,基本都是声明一个数组,把结果存到数组中,递归处理左右子树,判断边界情况返回。
eg: 257-二叉树的所有路径
