【亡羊补牢】挑战数据结构与算法 第48期 LeetCode 104. 二叉树的最大深度(二叉树)
发布日期:2021-06-29 14:34:26 浏览次数:3 分类:技术文章

本文共 1609 字,大约阅读时间需要 5 分钟。

仰望星空的人,不应该被嘲笑

题目描述

给定一个二叉树,找出其最大深度。

二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。

说明: 叶子节点是指没有子节点的节点。

示例:

给定二叉树 [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};

最后

文章产出不易,还望各位小伙伴们支持一波!

往期精选:

小伙伴们可以在Issues中提交自己的解题代码,🤝 欢迎Contributing,可打卡刷题,Give a ⭐️ if this project helped you!

,方便小伙伴阅读玩耍~

学如逆水行舟,不进则退

转载地址:https://chocolate.blog.csdn.net/article/details/108796219 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:【亡羊补牢】挑战数据结构与算法 第49期 LeetCode 199. 二叉树的右视图(二叉树)
下一篇:【亡羊补牢】挑战数据结构与算法 第47期 LeetCode 111. 二叉树的最小深度(二叉树)

发表评论

最新留言

路过按个爪印,很不错,赞一个!
[***.219.124.196]2024年04月23日 03时52分52秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章