剑指offer55
求二叉树的深度
精髓:
1.知道递归的方式,树的遍历顺序,这里是前序遍历
2.这里的递归没有返回值,只有递没有归。更简单
3.将当前执行函数的值传递给下一次递归的函数
var maxDepth = function(root) {
//let res = [];
let maxRows =0;
function sum(root,rows){
if (root==null)
return
sum(root.left,rows+1)
sum(root.right,rows+1)
if(root.left==null&&root.right==null){
maxRows = maxRows>rows?maxRows:rows
}
}
sum(root,1)
return maxRows
};
相似题目
求二叉树路径之和