leetcode-javascript
leetcode-javascript copied to clipboard
LeetCode 104. 二叉树的最大深度
仰望星空的人,不应该被嘲笑
题目描述
给定一个二叉树,找出其最大深度。
二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。
说明: 叶子节点是指没有子节点的节点。
示例:
给定二叉树 [3,9,20,null,null,15,7],
3
/ \
9 20
/ \
15 7
返回它的最大深度 3 。
来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/maximum-depth-of-binary-tree 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解题思路
dfs
,通过后续遍历求得,但是需要注意最后结果要加上根节点(即加1),并且需要判断一下树是不是为空,树为空的话直接返回 0
即可,不加 1
。
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {number}
*/
var maxDepth = function (root) {
if(!root) return 0;
let dfs = (root) => {
if (root == null) return 0;
// 后续遍历
let left = root.left && dfs(root.left) + 1;
let right = root.right && dfs(root.right) + 1;
return Math.max(left, right);
}
// 后续遍历结果还要加上根节点(即加1)
return dfs(root) + 1;
};
解法二
bfs
,一层一层访问,每加一层计数器就加1,这样到达最后一层了直接返回我们的计数器结果即可。
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {number}
*/
var maxDepth = function(root) {
if(!root) return 0
let queue =[root]
let cnt = 0
while(queue.length){
let size = queue.length
while(size--){
let node = queue.shift()
if(node.left) queue.push(node.left)
if(node.right) queue.push(node.right)
}
++cnt
}
return cnt
};
最后
文章产出不易,还望各位小伙伴们支持一波!
往期精选:
leetcode-javascript:LeetCode 力扣的 JavaScript 解题仓库,前端刷题路线(思维导图)
小伙伴们可以在Issues中提交自己的解题代码,🤝 欢迎Contributing,可打卡刷题,Give a ⭐️ if this project helped you!
访问超逸の博客,方便小伙伴阅读玩耍~
学如逆水行舟,不进则退