提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
前言
一、力扣530. 二叉搜索树的最小绝对差
递归双指针法
/**
* 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 = null;
int diff = Integer.MAX_VALUE;
public int getMinimumDifference(TreeNode root) {
inOrder(root);
return diff;
}
public void inOrder(TreeNode root){
if(root == null){
return ;
}
inOrder(root.left);
if(pre == null){
pre = root;
}else{
diff = Math.min(Math.abs(pre.val - root.val), diff);
pre = root;
}
inOrder(root.right);
}
}
迭代双指针法
/**
* 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 {
public int getMinimumDifference(TreeNode root) {
TreeNode pre = null;
int diff = Integer.MAX_VALUE;
Deque<TreeNode> deq = new LinkedList<>();
TreeNode p = root;
while(!deq.isEmpty() || p != null){
if(p != null){
deq.offerLast(p);
p = p.left;
}else{
p = deq.pollLast();
if(pre == null){
pre = p;
}else{
diff = Math.min(diff, Math.abs(pre.val - p.val));
pre = p;
}
p = p.right;
}
}
return diff;
}
}
二、力扣501. 二叉搜索树中的众数
递归
/**
* 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 {
ArrayList<Integer> resList;
int maxCount;
int count;
TreeNode pre;
public int[] findMode(TreeNode root) {
resList = new ArrayList<>();
maxCount = 0;
count = 0;
pre = null;
findMode1(root);
int[] res = new int[resList.size()];
for (int i = 0; i < resList.size(); i++) {
res[i] = resList.get(i);
}
return res;
}
public void findMode1(TreeNode root) {
if (root == null) {
return;
}
findMode1(root.left);
int rootValue = root.val;
// 计数
if (pre == null || rootValue != pre.val) {
count = 1;
} else {
count++;
}
// 更新结果以及maxCount
if (count > maxCount) {
resList.clear();
resList.add(rootValue);
maxCount = count;
} else if (count == maxCount) {
resList.add(rootValue);
}
pre = root;
findMode1(root.right);
}
}
三、力扣236. 二叉树的最近公共祖先
/**
* 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 == p || root == q || root == null)return root;
TreeNode leftChild = lowestCommonAncestor(root.left, p, q);
TreeNode rightChild = lowestCommonAncestor(root.right, p, q);
if(leftChild == null && rightChild != null){
return rightChild;
}else if(rightChild == null && leftChild != null){
return leftChild;
}else if(rightChild != null &&rightChild != null){
return root;
}else{
return null;
}
}
}