Finding the Lowest Common Ancestor in a Binary Tree
Minimum Absolute Difference in a Binary Search Tree
To compute the minimum absolute difference between values in a binary search tree, leverage the property that an in-order traversal yields a sorted sequence. By tracking the previous node during traversal, differences can be calculatde efficiently.
class Solution {
TreeNode previous = null;
int minDiff = Integer.MAX_VALUE;
public int getMinDiff(TreeNode node) {
if (node == null) return 0;
getMinDiff(node.left);
if (previous != null) {
minDiff = Math.min(minDiff, node.val - previous.val);
}
previous = node;
getMinDiff(node.right);
return minDiff;
}
}
Finding Modes in a Binary Search Tree
Approach 1: Using a Map
Traevrse the tree to collect values, then use a frequency map to identify the most common values.
class Solution {
public int[] findModes(TreeNode root) {
List<Integer> values = new ArrayList<>();
inorderCollect(root, values);
Map<Integer, Integer> freqMap = new HashMap<>();
for (int val : values) {
freqMap.put(val, freqMap.getOrDefault(val, 0) + 1);
}
int maxFreq = 0;
List<Integer> result = new ArrayList<>();
for (Map.Entry<Integer, Integer> entry : freqMap.entrySet()) {
if (entry.getValue() > maxFreq) {
maxFreq = entry.getValue();
result.clear();
result.add(entry.getKey());
} else if (entry.getValue() == maxFreq) {
result.add(entry.getKey());
}
}
return result.stream().mapToInt(i -> i).toArray();
}
private void inorderCollect(TreeNode node, List<Integer> list) {
if (node == null) return;
inorderCollect(node.left, list);
list.add(node.val);
inorderCollect(node.right, list);
}
}
Approach 2: In-Place Traversal
Perform an in-order traversal while maintaining counts to find modes with out extra storage for all values.
class Solution {
TreeNode prev = null;
int currentCount = 0;
int maxCount = 0;
List<Integer> modes = new ArrayList<>();
public int[] findModes(TreeNode root) {
traverse(root);
int[] result = new int[modes.size()];
for (int i = 0; i < modes.size(); i++) {
result[i] = modes.get(i);
}
return result;
}
private void traverse(TreeNode node) {
if (node == null) return;
traverse(node.left);
if (prev == null || prev.val != node.val) {
currentCount = 1;
} else {
currentCount++;
}
if (currentCount > maxCount) {
maxCount = currentCount;
modes.clear();
modes.add(node.val);
} else if (currentCount == maxCount) {
modes.add(node.val);
}
prev = node;
traverse(node.right);
}
}
Lowest Common Ancestor in a Binary Tree
To find the lowest common ancestor of two nodes in a binary tree, use a post-order traversal to search from the bottom up. The algorithm recursively checks each node to determine if it contains the target nodes in its subtrees.
class Solution {
public TreeNode findLCA(TreeNode node, TreeNode p, TreeNode q) {
if (node == null) return null;
if (node == p || node == q) return node;
TreeNode leftResult = findLCA(node.left, p, q);
TreeNode rightResult = findLCA(node.right, p, q);
if (leftResult != null && rightResult != null) {
return node;
}
if (leftResult != null) return leftResult;
if (rightResult != null) return rightResult;
return null;
}
}