530. 二叉搜索树的最小绝对差
注意是二叉搜索树。
最直观的做法,就是中序遍历二叉搜索树,最小差值一定出现在中序遍历序列的相邻元素之间。
class Solution {
TreeNode preNode;
int res = Integer.MAX_VALUE;
public int getMinimumDifference(TreeNode root) {
inOrder(root);
return res;
}
//中序遍历二叉搜索树
public void inOrder(TreeNode root){
if(root == null){
return;
}
inOrder(root.left);
if(preNode != null){
res = Math.min(res, root.val - preNode.val);
}
preNode = root;
inOrder(root.right);
}
}
——————————————————————————————————————————
501. 二叉搜索树中的众数
中序遍历,使用一个count记录节点值的出现次数,当当前节点和上一个节点的值相等时,count+1,否则count重置为1。
每遍历一个节点,判断一下count和以前记录的最大次数nowMaxCount的大小,若count更大,则将结果集合中的所有元素移除,并将上一个节点的元素加入集合
若nowMaxCount更大,不做操作。若一样大,则将上一个节点的值加入集合
class Solution {
List<Integer> resList = new ArrayList<>();
int nowMaxCount = 1;
int count = 1;
TreeNode preNode;
public int[] findMode(TreeNode root) {
inOrderFindFunc(root);
return resList.stream()
.filter(Objects::nonNull)
.mapToInt(value -> value)
.toArray();
}
private void inOrderFindFunc(TreeNode root){
if(root == null){
return;
}
inOrderFindFunc(root.left);
if(preNode != null){
if(root.val == preNode.val){
count++;
}else {
count = 1;
}
if(count > nowMaxCount){
resList.clear();
resList.add(root.val);
nowMaxCount = count;
} else if (count == nowMaxCount) {
resList.add(root.val);
}
}else {
resList.add(root.val);
}
preNode = root;
inOrderFindFunc(root.right);
}
}
——————————————————————————————————————————
236. 二叉树的最近公共祖先
自己思考的版本,逻辑有点混乱
class Solution {
//肯定要遍历二叉树,先确定一下用哪种遍历方式。要先遍历到p、q节点,再遍历到祖先节点,那么应该用后序遍历
//然后确定一下中间节点如何处理
boolean findCommonAncestor = false;
TreeNode res;
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
lowestCommonAncestorFunc(root, p , q);
return res;
}
private TreeNode lowestCommonAncestorFunc(TreeNode root, TreeNode p, TreeNode q) {
//后序遍历
//使用一个布尔变量findCommonAncestor,来标识是否已经找到了近公共祖先。找到最近公共祖先后,findCommonAncestor就置为true。
//p或者q即为最近公共祖先的情况:遍历到p、q任一节点时,查看当前节点的左右子树是否已经遍历到了另一个节点,如果遍历到了,就将结果节点置为当前节点
//p、q不为最近公共祖先的情况:对中间节点的处理就是,当左右子树的返回值都不为空,说明p和q分别在当前节点的左右字数上,此时若使用一个布尔变量findCommonAncestor还为false,即说明当前节点即为最近公共祖先,将结果节点置为当前节点。
//当前返回值的情况:当左子树的返回值不为空时,返回左子树的返回值。当右子树的返回值不为空时,返回右子树的返回值。
//其余情况返回null
if(root == null){
return null;
}
TreeNode leftNode = lowestCommonAncestorFunc(root.left, p, q);
TreeNode rightNode = lowestCommonAncestorFunc(root.right, p, q);
if(root.val == p.val){
if(leftNode != null || rightNode != null) {
findCommonAncestor = true;
res = root;
}
return root;
}
if(root.val == q.val){
if(leftNode != null || rightNode != null) {
findCommonAncestor = true;
res = root;
}
return root;
}
if(leftNode != null && rightNode != null && !findCommonAncestor){
findCommonAncestor = true;
res = root;
return root;
}
if(leftNode != null){
return leftNode;
}
if(rightNode != null){
return rightNode;
}
return null;
}
}
代码随想录的解法
class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
//后序遍历
//重新整理一下逻辑
//中间节点的处理方案:当左右子树的返回值都不为null时,说明找到了最近公共祖先,返回当前节点root
//当左右子树的返回值有一个不为null时,返回不为null的节点
//当左右子树的返回值都为null时,返回null
//递归终止条件:当root == null或者p == root或者q == root时,返回当前节点
if(root == null || root.val == p.val || root.val == q.val){
return root;//这里包含了p、q其中一个节点为最近公共祖先的情况
}
TreeNode leftNode = lowestCommonAncestor(root.left, p, q);
TreeNode rightNode = lowestCommonAncestor(root.right, p, q);
if(leftNode == null && rightNode == null){
return null;
} else if (leftNode == null && rightNode != null) {
return rightNode;
} else if (leftNode != null && rightNode == null) {
return leftNode;
}else {
return root;
}
}
}