235 Lowest Common Ancestor of a Binary Search Tree – Easy
Problem:
_______6______
/ \
___2__ ___8__
/ \ / \
0 _4 7 9
/ \
3 5Thoughts:
Solutions:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
int min = Math.min(p.val, q.val);
int max = Math.max(p.val, q.val);
return lca(root, min, max);
}
private TreeNode lca(TreeNode root, int min, int max) {
if (root.val >= min && root.val <= max) {
return root;
}
if (root.val > max) {
return lca(root.left, min, max);
}
if (root.val < min) {
return lca(root.right, min, max);
}
return null;
}
}Last updated