今日任务
530.二叉搜索树的最小绝对差
501.二叉搜索树中的众数
236. 二叉树的最近公共祖先
530.二叉搜索树的最小绝对差
题目链接: . - 力扣(LeetCode)
/**
* 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 min = Integer.MAX_VALUE;
public int getMinimumDifference(TreeNode root) {
/**
*
* 递归中序遍历
*
* */
midtravel(root);
return min;
}
public void midtravel(TreeNode root){
if (root == null) {return;}
midtravel(root.left);
if (pre != null) {
min = Math.min(min, root.val - pre.val);
}
pre = root;
midtravel(root.right);
return;
}
}
/**
* 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) {
/**
* 中序输出的数组是从小到达的顺序,再使用双指针法
* 统一迭代法中序输出
* */
Stack<TreeNode> stack = new Stack<TreeNode>();
int res = Integer.MAX_VALUE;
if (root == null) return res;
int pre = Integer.MAX_VALUE;
List<Integer> resList = new ArrayList<>();
stack.push(root);
while (!stack.isEmpty()) {
if (stack.peek() != null) {
TreeNode node = stack.pop();
if (node.left != null) {stack.push(node.left);}
stack.push(node);
stack.push(null);
if (node.right != null) {stack.push(node.right);}
} else {
stack.pop();
TreeNode node = stack.pop();
res = Math.min(res, Math.abs(node.val - pre));
pre = node.val;
}
}
return res;
}
}
/**
* 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) {
/**
* 中序输出的数组是从小到达的顺序,再使用双指针法
* 统一迭代法中序输出
* */
Stack<TreeNode> stack = new Stack<TreeNode>();
int res = Integer.MAX_VALUE;
if (root == null) return res;
List<Integer> resList = new ArrayList<>();
stack.push(root);
while (!stack.isEmpty()) {
if (stack.peek() != null) {
TreeNode node = stack.pop();
if (node.left != null) {stack.push(node.left);}
stack.push(node);
stack.push(null);
if (node.right != null) {stack.push(node.right);}
} else {
stack.pop();
TreeNode node = stack.pop();
resList.add(node.val);
}
}
int[] resArray = new int[resList.size()];
for (int i = 0; i < resList.size(); i++) {
resArray[i] = resList.get(i);
}
for (int i = 0; i < resList.size() - 1; i++) {
res = Math.min(res, Math.abs(resArray[i] - resArray[i + 1]));
}
return res;
}
}
501.二叉搜索树中的众数
题目链接: . - 力扣(LeetCode)
/**
* 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 {
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
public int[] findMode(TreeNode root) {
//递归法
travel(root);
ArrayList<int[]> list = new ArrayList();
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
list.add(new int[]{entry.getValue(), entry.getKey()});
}
list.sort((o1, o2)->{return o2[0] - o1[0];});
List<Integer> res = new ArrayList<>();
res.add(list.get(0)[1]);
for (int i = 1; i < list.size(); i++) {
if(list.get(i)[0] == list.get(0)[0]){
res.add(list.get(i)[1]);
} else {
break;
}
}
return res.stream().mapToInt(Integer::intValue).toArray();
}
public void travel(TreeNode root){
if (root == null){return;}
map.put(root.val, map.getOrDefault(root.val, 0) + 1);
travel(root.left);
travel(root.right);
return;
}
}
236. 二叉树的最近公共祖先
题目链接: . - 力扣(LeetCode)
/**
* 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) {
Stack<TreeNode> stack = new Stack<TreeNode>();
Stack<List<TreeNode>> pathstack = new Stack<List<TreeNode>>();
List<TreeNode> pList = new ArrayList<>();
List<TreeNode> qList = new ArrayList<>();
List<TreeNode> rootList = new ArrayList<>();
if (root == null) return root;
rootList.add(root);
stack.push(root);
pathstack.push(rootList);
while (!stack.isEmpty() && !pathstack.isEmpty()) {
List<TreeNode> tmpList = pathstack.pop();
TreeNode tmpNOde = (TreeNode)stack.pop();
if (tmpNOde.val == p.val) {pList = tmpList;}
if (tmpNOde.val == q.val) {qList = tmpList;}
if (tmpNOde.right != null) {
stack.push(tmpNOde.right);
List<TreeNode> leftList = new ArrayList<>();
leftList.addAll(tmpList);
leftList.add(tmpNOde.right);
pathstack.push(leftList);
}
if (tmpNOde.left != null) {
stack.push(tmpNOde.left);
List<TreeNode> leftList = new ArrayList<>();
leftList.addAll(tmpList);
leftList.add(tmpNOde.left);
pathstack.push(leftList);
}
}
for (TreeNode node:pList){
System.out.println("The pList is " + node.val);
}
for (TreeNode node:qList){
System.out.println("The qList is " + node.val);
}
Set<TreeNode> set = new HashSet<>();
for (TreeNode node:pList){
set.add(node);
}
for (int i = qList.size() - 1; i >= 0; i--) {
if (set.contains(qList.get(i))) {
return qList.get(i);
}
}
return null;
}
}
/**
* 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;
}
}
}