104.二叉树的最大深度
- 刷题
https://leetcode.cn/problems/maximum-depth-of-binary-tree/description/
- 文章讲解
https://programmercarl.com/0104.%E4%BA%8C%E5%8F%89%E6%A0%91%E7%9A%84%E6%9C%80%E5%A4%A7%E6%B7%B1%E5%BA%A6.html
- 视频讲解
https://www.bilibili.com/video/BV1Gd4y1V75u/?vd_source=af4853e80f89e28094a5fe1e220d9062
-
题解(递归法DFS):
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
//DFS递归解法
public int getdepth(TreeNode cur){
if(cur == null){
return 0;
}
//必须使用后序处理,处理完左右结点后,
//带着左右孩子的信息返回给中间结点
int leftdepth = getdepth(cur.left);
int rightdepth = getdepth(cur.right);
int depth = Math.max(leftdepth, rightdepth) + 1;
return depth;
}
public int maxDepth(TreeNode root) {
return getdepth(root);
}
}
111.二叉树的最小深度
- 刷题
https://leetcode.cn/problems/minimum-depth-of-binary-tree/description/
- 文章讲解
https://programmercarl.com/0111.%E4%BA%8C%E5%8F%89%E6%A0%91%E7%9A%84%E6%9C%80%E5%B0%8F%E6%B7%B1%E5%BA%A6.html
- 视频讲解
https://www.bilibili.com/video/BV1QD4y1B7e2/?vd_source=af4853e80f89e28094a5fe1e220d9062
-
题解(递归法DFS):
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
//DFS递归求最小深度
public int getminDepth(TreeNode cur){
if(cur == null){
return 0;
}
int leftDepth = getminDepth(cur.left);
int rightDepth = getminDepth(cur.right);
//如果有一个子树为空,则最小高度取非空子树高度+1
if(cur.left == null && cur.right != null){
return rightDepth + 1;
}
if(cur.right == null && cur.left != null){
return leftDepth + 1;
}
//如果两个子树均不为空
int result = Math.min(leftDepth, rightDepth) + 1;
return result;
}
//主函数
public int minDepth(TreeNode root) {
return getminDepth(root);
}
}
222.完全二叉树的节点个数
- 刷题
https://leetcode.cn/problems/count-complete-tree-nodes/description/
- 文章讲解
https://programmercarl.com/0222.%E5%AE%8C%E5%85%A8%E4%BA%8C%E5%8F%89%E6%A0%91%E7%9A%84%E8%8A%82%E7%82%B9%E4%B8%AA%E6%95%B0.html
- 视频讲解
https://www.bilibili.com/video/BV1eW4y1B7pD/?vd_source=af4853e80f89e28094a5fe1e220d9062
-
移位介绍:
2 << leftDepth
该代码的意思是将数字2向左移动leftDepth位。
在二进制中,向左移动一位相当于乘以2,向左移动n位相当于乘以2的n次方。
-
题解(递归法DFS):
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
//递归DFS解法
public int countNodes(TreeNode root) {
if(root == null){
return 0;
}
TreeNode curleft = root.left;
TreeNode curright = root.right;
int leftDepth = 0, rightDepth = 0;
//求左子树左分支深度
while(curleft != null){
curleft = curleft.left;
leftDepth++;
}
//求右子树右分支深度
while(curright != null){
curright = curright.right;
rightDepth++;
}
//若做右分支深度相同,又因为二叉树为完全二叉树
//所以此时当前二叉树一定为满二叉树
//此时利用满二叉树结点总数公式进行计算
if(leftDepth == rightDepth){
return (2 << leftDepth) - 1;
}
//如果做右分支深度不同,则当前二叉树并不是满二叉树
//则递归,继续向下遍历
//因为最坏情况下会遍历到叶子结点,而叶子结点必然是满二叉树,左右分支深度均为0
//所以向下递归遍历下去一定会得到满二叉树,然后计算当前结点总数并逐层返回
return countNodes(root.left) + countNodes(root.right) + 1;
}
}