代码随想录算法训练营第十六天 | 104.二叉树的最大深度 (优先掌握递归)、 111.二叉树的最小深度 (优先掌握递归)、 222.完全二叉树的节点个数(优先掌握递归)
104.二叉树的最大深度 (优先掌握递归)
题目链接:104. 二叉树的最大深度 - 力扣(LeetCode)
同样是利用递归法和迭代法解决
- 递归法:正常前序递归,维护一个maxDepth
class Solution {
int max;
public int maxDepth(TreeNode root) {
max = 0;
dfs(root, 0);
return max;
}
public void dfs(TreeNode node, int count) {
if(node == null) return;
count++;
max = Math.max(max, count);
dfs(node.left, count);
dfs(node.right, count);
}
}
- 迭代法:层序遍历,不断更新depth
class Solution {
public int maxDepth(TreeNode root) {
if(root == null) return 0;
Queue<TreeNode> q = new LinkedList<>();
q.offer(root);
int count = 0;
while(!q.isEmpty()) {
int len = q.size();
count++;
for(int i = 0; i < len; i++) {
TreeNode node = q.poll();
if(node.left != null) q.offer(node.left);
if(node.right != null) q.offer(node.right);
}
}
return count;
}
}
111.二叉树的最小深度 (优先掌握递归)
题目链接:111. 二叉树的最小深度 - 力扣(LeetCode)
- 递归法:同样也是前序遍历,然后维护minDepth, 只要一个节点是叶子节点就更新minDepth
class Solution {
int min_d;
public int minDepth(TreeNode root) {
min_d = Integer.MAX_VALUE;
if(root == null) return 0;
dfs(root, 0);
return min_d;
}
public void dfs(TreeNode node, int count) {
if(node == null) return;
count++;
if(node.left == null && node.right == null) {
// 只要是叶子节点,就更新minDepth
min_d = Math.min(min_d, count);
// 因为无法向下递归,直接return
return;
} else {
dfs(node.left, count);
dfs(node.right, count);
}
}
}
- 迭代法:层序遍历,也是维护一个minDepth,在每一层中寻找是否有叶子节点,更新minDepth
class Solution {
public int minDepth(TreeNode root) {
if(root == null) return 0;
Queue<TreeNode> q = new LinkedList<>();
q.offer(root);
int mind = Integer.MAX_VALUE;
int count = 0;
while(!q.isEmpty()) {
int len = q.size();
count++;
for(int i = 0; i < len; i++) {
TreeNode node = q.poll();
if(node.left == null && node.right == null) mind = Math.min(count, mind); // 只要是叶子节点,判断当前层数和minDepth大小
// 常规层序遍历逻辑处理
if(node.left != null) q.offer(node.left);
if(node.right != null) q.offer(node.right);
}
}
return mind;
}
}
222.完全二叉树的节点个数(优先掌握递归)
题目链接:222. 完全二叉树的节点个数 - 力扣(LeetCode)
- 递归法:就是简单的递归前序遍历,用一个count计数,只要node != null 便 ++
class Solution {
int count;
public int countNodes(TreeNode root) {
count = 0;
dfs(root);
return count;
}
void dfs(TreeNode node) {
if(node == null) return;
count++;
dfs(node.left);
dfs(node.right);
}
}
- 迭代法:层序遍历?更简单了,直接sum += len,len为每层迭代的节点个数
class Solution {
public int countNodes(TreeNode root) {
if(root == null) return 0;
Queue<TreeNode> q = new LinkedList<>();
q.offer(root);
int sum = 0;
while(!q.isEmpty()) {
int len = q.size();
for(int i = 0; i < len; ++i) {
TreeNode node = q.poll();
if(node.left != null) q.offer(node.left);
if(node.right != null) q.offer(node.right);
}
sum += len;
}
return sum;
}
}
总结:
- 二叉树:主要是又把层序遍历复习了。递归前序遍历我基本上已经没问题了。现在二叉树的翻转和是否对称
对我来说可能还要难些
- 今天自己复习了一下数组+双指针,主要是以下题目:
快慢指针法:用一个index(慢指针)表示待更新的新数组下标,fast指针找到一个 != val的值就nums[index++]
26. 删除有序数组中的重复项 - 力扣(LeetCode)
同上,关键逻辑是
int index = 1, fast = 1 // index指向新数组待更新的下标
while(fast < n) {
if(nums[fast] != nums[fast - 1]) nums[index++] = nums[fast];
fast++;
}
快慢指针:用一个index(慢指针)表示待更新的新数组下标,fast指针找到一个 != 0的值就交换nums[index] 和 nums[fast]同时index++
这题比较难。后面可能再做一下,使用栈实现对我来说比较好理解
双指针从两端开始(因为两端对应着的平方值可能比中间更大),新数组的index从尾部开始减减
80. 删除有序数组中的重复项 II - 力扣(LeetCode)
这题是我自己方法做的
int count = 0; // 用一个count记录当前相同元素连续的个数
int index = 1, fast = 1; // index指向新数组待更新的下标
while(fast < n) {
if(nums[fast] == nums[fast - 1]) {
count++;
if(count < 2) {
nums[index++] = nums[fast]; // 注意这里必须要更新index值,不要因为一开始index和fast指向的都是相同的元素就觉得index++就行了。后续随着移除的重复项越多,index和fast会指向不同的元素
}
} else {
count = 0; // count设为0,注意细节
nums[index++] = nums[fast];
}
fast++;
}
加油zyc!