学习目标:
60天训练营打卡计划!
还是要按照基本的流程来写递归函数,否则就会迷茫。。。
学习内容:
530.二叉搜索树的最小绝对差
- 递归法处理
- 因为是二叉搜索树,使用中序遍历后得到的结果是有序数组。
- 搜索二叉树常见的操作,新定义一个名为pre的TreeNode节点用来表示前一个节点。
class Solution {
TreeNode pre = null;
int res = Integer.MAX_VALUE;
// 递归的返回值为空,因为要遍历整棵树,去得到最小的节点差值
// 递归的参数为节点即可
// 搜索二叉树使用中序遍历即可获得一个有序的输出
private void traversal(TreeNode root) {
if(root == null) return;
// 左
traversal(root.left);
// 使用双指针处理 中
if(pre!= null){
res = Math.min(res, root.val - pre.val);
}
pre = root;
// 右
traversal(root.right);
}
public int getMinimumDifference(TreeNode root) {
traversal(root);
return res;
}
}
501.二叉搜索树中的众数
- 迭代法
- 比较难想的是中间节点的处理过程;可分为统计过程,双指针移动过程和更新过程。
- 统计过程:对pre为null和双节点值不等时,count赋1;相等时,count++
- 节点移动过程:pre = root;
- 更新过程:当当前统计的数量等于最高频时,将节点的值加入结果数组;当当前统计的数量大于最高频时,清空结果数组,并添加当前的节点。
class Solution {
List<Integer> res = new ArrayList<>();
int maxCount = 0;
int count = 0;
TreeNode pre = null;
private void traversal(TreeNode root){
if(root == null) return;
traversal(root.left);
if(pre == null || pre.val != root.val) count = 1;
else if(pre.val == root.val) count++;
// 如果pre指针不移动,那么整棵树都会被加入到res中
pre = root;
if(count == maxCount) res.add(root.val);
else if(count > maxCount){
maxCount = count;
res.clear();
res.add(root.val);
}
traversal(root.right);
return;
}
public int[] findMode(TreeNode root) {
traversal(root);
int[] tmp = new int[res.size()];
for(int i = 0; i < res.size(); i++){
tmp[i] = res.get(i);
}
return tmp;
}
}
236. 二叉树的最近公共祖先
- 该题只有三种情况:
- ①题目中给定的p和q分别在左右子树,那么最近公共祖先一定是根节点。
-
- `// 若左右节点分别对应要找的节点,则返回公共祖先root节点。
if(left != null && right != null) return root;
- `// 若左右节点分别对应要找的节点,则返回公共祖先root节点。
- ②题目中给定的p和q在同一棵子树上,那么最近公共祖先一定是个左右子树上的某个节点。
-
- `// 若只有一侧返回了节点,那么该节点就是公共祖先。
if(left != null && right == null) return root.left;
if(left == null && right != null) return root.right;
- `// 若只有一侧返回了节点,那么该节点就是公共祖先。
- ③没有找到公共祖先,返回null。
class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
// 递归的终止条件
if(root == null) return root;
// 通过回溯获得左右节点上传的信息
if(root == p || root == q) return root;
// 使用后序遍历,中节点的通过回溯获得左右节点上传的信息
// 左子树有没有p或q
TreeNode left = lowestCommonAncestor(root.left, p, q);
// 右子树有没有p或q
TreeNode right = lowestCommonAncestor(root.right, p, q);
// 中:处理流程
// 若左右节点分别对应要找的节点,则返回公共祖先root节点。
if(left != null && right != null) return root;
// 若只有一侧返回了节点,那么该节点就是公共祖先。
if(left != null && right == null) return root.left;
if(left == null && right != null) return root.right;
// 找不到公共祖先
return null;
}
}
学习时间:
- 上午两个半小时,下午一个小时(认真梳理了236的逻辑),整理文档半小时。
- 2024.2.22二刷