今日内容:
- 104.二叉树的最大深度 559.n叉树的最大深度
- 111.二叉树的最小深度
- 222.完全二叉树的节点个数
104.二叉树的最大深度
给定一个二叉树,找出其最大深度。
二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。
说明: 叶子节点是指没有子节点的节点。
示例: 给定二叉树 [3,9,20,null,null,15,7],
返回它的最大深度 3 。
#算法公开课
《代码随想录》算法视频公开课 (opens new window):二叉树的高度和深度有啥区别?究竟用什么遍历顺序?很多录友搞不懂 | 104.二叉树的最大深度 (opens new window),相信结合视频再看本篇题解,更有助于大家对本题的理解。
题解: 这个题之前用层序遍历写出来过,现在用后序递归法再做一次
层序遍历
class Solution {
public int maxDepth(TreeNode root) {
int result = 0;
if(root == null) return result;
Queue<TreeNode> que = new LinkedList<>();
que.offer(root);
while(!que.isEmpty()){
int len = que.size();
while(len > 0){
TreeNode tmpNode = que.poll();
if(tmpNode.left != null)que.offer(tmpNode.left);
if(tmpNode.right != null)que.offer(tmpNode.right);
len--;
}
result++;
}
return result;
}
}
后序遍历,递归
class Solution {
public int maxDepth(TreeNode root) {
if(root == null) return 0;
int leftHeight = maxDepth(root.left);
int rightHeight = maxDepth(root.right);
int height = 1+ Math.max(leftHeight,rightHeight);
return height;
}
}
111.二叉树的最小深度
给定一个二叉树,找出其最小深度。
最小深度是从根节点到最近叶子节点的最短路径上的节点数量。
说明: 叶子节点是指没有子节点的节点。
示例:
给定二叉树 [3,9,20,null,null,15,7],
返回它的最小深度 2.
#算法公开课
《代码随想录》算法视频公开课 (opens new window):看起来好像做过,一写就错! | LeetCode:111.二叉树的最小深度 (opens new window),相信结合视频再看本篇题解,更有助于大家对本题的理解。
这个题我原来是用层序遍历的方法写的
class Solution {
public int minDepth(TreeNode root) {
int result = 0;
if(root == null) return result;
Queue<TreeNode> que = new LinkedList<>();
que.offer(root);
result++;
while(!que.isEmpty()){
int len = que.size();
while(len > 0){
TreeNode tmpNode = que.poll();
if(tmpNode.left == null && tmpNode.right == null){
return result;
}
else{
if(tmpNode.left != null){
que.offer(tmpNode.left);
}
if(tmpNode.right != null){
que.offer(tmpNode.right);
}
}
len--;
}
result++;
}
return result;
}
}
这是后序递归遍历的代码,注意这里的判断逻辑要区别开左右分枝为null的情况, 如果不区分的话, 如果一个分支的一边为空, 这个分支就会被判定为叶子,但实际上是错误的.
class Solution {
public int minDepth(TreeNode root) {
if(root == null) return 0;
int left = minDepth(root.left);
int right = minDepth(root.right);
if(root.left == null && root.right != null){
return 1+ right;
}
else if(root.right == null && root.left != null){
return 1+ left;
}
else{
return 1+ Math.min(left,right);
}
}
}
222.完全二叉树的节点个数
给出一个完全二叉树,求出该树的节点个数。
示例 1:
- 输入:root = [1,2,3,4,5,6]
- 输出:6
示例 2:
- 输入:root = []
- 输出:0
示例 3:
- 输入:root = [1]
- 输出:1
提示:
- 树中节点的数目范围是[0, 5 * 10^4]
- 0 <= Node.val <= 5 * 10^4
- 题目数据保证输入的树是 完全二叉树
#算法公开课
《代码随想录》算法视频公开课 (opens new window):要理解普通二叉树和完全二叉树的区别! | LeetCode:222.完全二叉树节点的数量 (opens new window),相信结合视频再看本篇题解,更有助于大家对本题的理解。
解答: 利用完全二叉树中含有满二叉树的特性, 遍历左右两侧分支而不是遍历所有分支来减少计算量.这个题用纯递归, 层序遍历也都能做. 注意二次方的表现形式是 2<<x,例如2的5次方就是2<<5.
class Solution {
public int countNodes(TreeNode root) {
//利用完全二叉树的特性来解题,找里面的满二叉树
if(root == null) return 0;
TreeNode left = root.left;
TreeNode right = root.right;
int leftDepth = 0;
int rightDepth = 0;
while(left!= null){
left = left.left;
leftDepth++;
}
while(right!= null){
right = right.right;
rightDepth++;
}
if(rightDepth == leftDepth){
return (2 << rightDepth) -1;
}
int leftTreeNum = countNodes(root.left);
int rightTreeNum = countNodes(root.right);
int result = leftTreeNum + rightTreeNum +1;
return result;
}
}