代码随想录算法训练营第十六天

编程入门 行业动态 更新时间:2024-10-13 02:20:32

代码随想录<a href=https://www.elefans.com/category/jswz/34/1770096.html style=算法训练营第十六天"/>

代码随想录算法训练营第十六天

二叉树相关

今天的题目按照递归做法基本是一种套路

104.二叉树的最大深度

参考文章
题目链接

个人题解

class Solution {public int maxDepth(TreeNode root) {if (root == null) {return 0;}return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;}
}

111.二叉树的最小深度

参考文章
题目链接

个人题解

class Solution {public int minDepth(TreeNode root) {if (root == null) {return 0;}int leftDepth = minDepth(root.left);int rightDepth = minDepth(root.right);return Math.min(leftDepth, rightDepth) + 1;}
}

222.完全二叉树的节点个数

参考文章
题目链接

个人题解

class Solution {public int countNodes(TreeNode root) {if (root == null) {return 0;}return countNodes(root.left) + countNodes(root.right) + 1;}}

更多推荐

代码随想录算法训练营第十六天

本文发布于:2024-02-06 02:41:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1745759.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:算法   训练营   六天   代码   随想录

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!