目录
530.二叉搜索树的最小绝对值差
解题思路
-
中序遍历-二叉搜索树是一个递增的数组
-
定义一个prev指针,指向当前节点的前一个节点,定义一个Integer最小值res=Integer.MIN_VALUE
-
中序:Math.min(res, root.val-prev.val)取得最小值
Java代码
/**
* 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 {
TreeNode pre; //记录前一个节点
int res = Integer.MAX_VALUE; //返回值,默认是INT最大值
//中序遍历-递归
public int getMinimumDifference(TreeNode root) {
if (root == null) {
return 0;
}
getMinimumDifference(root.left); //左
if (pre != null) {
res = Math.min(res, root.val - pre.val); //中
}
pre = root; //赋值当前节点给前一个节点
getMinimumDifference(root.right); //右
return res;
}
}
501.二叉搜索树中的众数
解题思路
-
递归遍历-中序遍历
-
确定返回值和参数:返回值可以有多个众数,int[]数组;参数TreeNode
-
确定终止条件:
-
root==null; return;
-
-
确定单层逻辑:
-
左:递归遍历左节点
-
中:节点计数,如果节点为null或者当前节点不等前一个节点值,则count=1;否则count++
-
保存高频率节点,如果count>maxCount,则清空resList,添加当前节点到resList,maxCount更新;如果count==maxCount,则把当前节点添加到resList
-
prev指针指向当前节点
-
-
右:递归遍历右节点
-
Java代码
/**
* 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 {
TreeNode prev; //保存前一个节点
List<Integer> resList; //保存频率高的节点
int maxCount; //节点的最大频率
int count; //节点的频率
public int[] findMode(TreeNode root) {
resList = new ArrayList<>();
maxCount = 0;
count = 0;
findMode1(root);
//List转int数组
return resList.stream().mapToInt(x->x).toArray();
}
//递归遍历 中序遍历
public void findMode1(TreeNode root) {
if (root == null) {
return;
}
findMode1(root.left); //左
int rootValue = root.val;
//计数
if (prev == null || rootValue != prev.val) {
count = 1;
} else {
count++;
}
//节点记录到resList
//如果count大于最大频率时,要清空resList,之前数组中的都失效了
if (count > maxCount) {
resList.clear();
resList.add(rootValue);
maxCount = count;
} else if (count == maxCount){
resList.add(rootValue);
}
//prev赋值
prev = root;
findMode1(root.right); //右
}
}
236.二叉树的最近公共祖先
解题思路
-
递归后序遍历-左右中
-
确定返回值和参数:返回值是节点TreeNode;参数是root
-
确定终止条件:root == null || root==p || root==q return root;
-
确定单层逻辑:
-
如果左节点为空&&右节点为空,则返回null
-
如果左节点为空&&右节点不为空,则返回右节点
-
如果左节点不为空&&右节点为空,则返回做节点
-
否则返回中间节点
-
Java代码
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
//递归-后序遍历
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
//终止条件
if (root == null || root == p || root == q) {
return root;
}
//左
TreeNode left = lowestCommonAncestor(root.left, p, q);
//右
TreeNode right = lowestCommonAncestor(root.right, p, q);
//中
if (left == null && right == null) {
return null;
} else if (left == null && right != null) {
return right;
} else if (left != null && right == null) {
return left;
} else {
return root;
}
}
}