530. Minimum Absolute Difference in BST
Given the root
of a Binary Search Tree (BST), return the minimum absolute difference between the values of any two different nodes in the tree.
Example 1:
Input: root = [4,2,6,1,3]
Output: 1
Example 2:
Input: root = [1,0,48,null,null,12,49]
Output: 1
class Solution {
public int min = Integer.MAX_VALUE;
public TreeNode pre;
public void traversal(TreeNode cur) {
if (cur == null) return;
traversal(cur.left);
if (pre != null) {
min = Math.min(min, cur.val-pre.val);
}
pre = cur;
traversal(cur.right);
}
public int getMinimumDifference(TreeNode root) {
traversal(root);
return min;
}
}
501. Find Mode in Binary Search Tree
Given the root
of a binary search tree (BST) with duplicates, return all the mode(s) (i.e., the most frequently occurred element) in it.
If the tree has more than one mode, return them in any order.
Assume a BST is defined as follows:
- The left subtree of a node contains only nodes with keys less than or equal to the node’s key.
- The right subtree of a node contains only nodes with keys greater than or equal to the node’s key.
- Both the left and right subtrees must also be binary search trees.
Example 1:
Input: root = [1,null,2,2]
Output: [2]
Example 2:
Input: root = [0]
Output: [0]
class Solution {
public TreeNode pre;
public int count = 0;
public int maxCount = 0;
public List<Integer> result = new ArrayList<>();
public void BST(TreeNode cur) {
if (cur == null) return;
BST(cur.left);
if (pre == null) count = 1;
else if (pre.val == cur.val) count++;
else count = 1;
pre = cur;
if (count == maxCount) result.add(cur.val);
if (count > maxCount) {
maxCount = count;
result.clear();
result.add(cur.val);
}
BST(cur.right);
}
public int[] findMode(TreeNode root) {
BST(root);
int size = result.size();
int[] arr = new int[size];
for (int i = 0; i < size; i++) {
arr[i] = result.get(i);
}
return arr;
}
}
236. Lowest Common Ancestor of a Binary Tree
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.
According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes p
and q
as the lowest node in T
that has both p
and q
as descendants (where we allow a node to be a descendant of itself).”
Example 1:
Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
Output: 3
Explanation: The LCA of nodes 5 and 1 is 3.
Example 2:
Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
Output: 5
Explanation: The LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.
Example 3:
Input: root = [1,2], p = 1, q = 2
Output: 1
class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if (root == null || root == p || root == q) return root;
TreeNode nodeLeft = lowestCommonAncestor(root.left, p, q);
TreeNode nodeRight = lowestCommonAncestor(root.right, p, q);
if (nodeLeft != null && nodeRight != null) return root;
if (nodeLeft != null) return nodeLeft;
return nodeRight;
}
}