You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.
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).”
Given binary search tree: root = [6,2,8,0,4,7,9,null,null,3,5]
Example 1:
Input: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 8
Output: 6
Explanation: The LCA of nodes 2 and 8 is 6.
Example 2:
Input: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 4
Output: 2
Explanation: The LCA of nodes 2 and 4 is 2, since a node can be a
descendant of itself according to the LCA definition.
Note:
All of the nodes' values will be unique.
p and q are different and both values will exist in the BST.
因为我刚做完求二叉树最近公共祖先的问题,用同样的解法:
class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
return process(root, p, q);
}
public TreeNode process(TreeNode node, TreeNode p, TreeNode q) {
if (node == null) return null;
TreeNode left = process(node.left, p, q);
TreeNode right = process(node.right, p, q);
if (left == null && right == null) {
if (node == p || node == q) return node;
else return null;
}
else if ((left == p && right == q)
|| (left == q && right == p)
|| (node == p && (left == q || right == q))
|| (node == q && (left== p || right == p))
)
return node;
else if ((left != null && right == null)
|| (left == null && right != null))
return left == null ? right : left;
else return null;
}
}
class Solution {
private TreeNode res = null;
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
int minV = Math.min(p.val, q.val);
int maxV = Math.max(p.val, q.val);
process(root, minV, maxV);
return res;
}
public void process(TreeNode node, int minV, int maxV) {
if (node == null) return;
int v = node.val;
if (v < minV) process(node.right, minV, maxV);
else if (v > maxV) process(node.left, minV, maxV);
else res = node;
}
}
非递归解法如下:
class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
TreeNode node = root;
if (node == null) return null;
while (true) {
if (node.val > Math.max(p.val, q.val)) node = node.left;
else if (node.val < Math.min(p.val, q.val)) node = node.right;
else break;
}
return node;
}
}
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.
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).”
Given binary search tree: root = [6,2,8,0,4,7,9,null,null,3,5]
Example 1:
Example 2:
Note:
因为我刚做完求二叉树最近公共祖先的问题,用同样的解法:
注意信息汇总的逻辑要清晰。
这道题没有那么复杂,要利用二叉搜索树(BST) 这点,即 左小右大。如果当前节点比题目所给节点最小值都小,则往右子树递归;如果当前节点比题目所给节点最大值都大,则往左子树递归;否则,当前节点就是最近公共祖先。
非递归解法如下:
参考资料:
相似题目:
236. Lowest Common Ancestor of a Binary Tree
The text was updated successfully, but these errors were encountered: