算法训练营第十六天"/>
代码随想录算法训练营第十六天
二叉树相关
今天的题目按照递归做法基本是一种套路
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;}}
更多推荐
代码随想录算法训练营第十六天
发布评论