Lowest Common Ancestor of a Binary Search Tree
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 v and w as the lowest node in T that has both v and w as descendants (where we allowa node to be a descendant of itself).”
_______6______
/ \
___2__ ___8__
/ \ / \
0 _4 7 9
/ \
3 5
For example, the lowest common ancestor (LCA) of nodes 2
and 8
is 6
. Another example is LCA of nodes 2
and 4
is 2
, since a node can be a descendant of itself according to the LCA definition.
思路:這題跟 Lowest Common Ancestor of Binary Tree 一模一樣。思路:就是找p的節點在不在左支,或者右支,找到各自左右節點,然後進行比較,如果兩者不一樣,說明當前的root就是lowest 父節點,如果左邊爲空,那就都在右邊,返回右邊的即可,如果右邊爲空,那就都在左邊,返回左邊的即可。
/**
* 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) {
return null;
}
if(p == null || q == null) {
return root;
}
TreeNode node = root;
while(node != null) {
if(node.val < p.val && node.val < q.val) {
node = node.right;
} else if(node.val > p.val && node.val > q.val) {
node = node.left;
} else {
break;
}
}
return node;
}
}
/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/
public class Solution {
/**
* @param root: root of the tree
* @param p: the node p
* @param q: the node q
* @return: find the LCA of p and q
*/
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if(root == null) {
return null;
}
if(p == root || q == root) {
return root;
}
if(p.val < root.val && q.val < root.val) {
return lowestCommonAncestor(root.left, p, q);
}
if(p.val > root.val && q.val > root.val) {
return lowestCommonAncestor(root.right, p, q);
}
return root;
}
}
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 v and w as the lowest node in T that has both v and w as descendants (where we allowa node to be a descendant of itself).”
_______3______
/ \
___5__ ___1__
/ \ / \
6 _2 0 8
/ \
7 4
For example, the lowest common ancestor (LCA) of nodes 5
and 1
is 3
. Another example is LCA of nodes 5
and 4
is 5
, since a node can be a descendant of itself according to the LCA definition.
思路:lowestCommonAncestor 的定義就是找到的LCA;
如果兩者都不爲空,說明當前的root就是lowest 父節點,如果左邊爲空,那就都在右邊,返回右邊的即可,如果右邊爲空,那就都在左邊,返回左邊的即可。
/**
* 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 leftnode = lowestCommonAncestor(root.left, p, q);
TreeNode rightnode = lowestCommonAncestor(root.right, p, q);
if(leftnode == null) {
return rightnode;
} else if(rightnode == null) {
return leftnode;
} else {
return root;
}
}
}
Given a rooted binary tree, return the lowest common ancestor of its deepest leaves.
Recall that:
- The node of a binary tree is a leaf if and only if it has no children
- The depth of the root of the tree is 0, and if the depth of a node is
d
, the depth of each of its children isd+1
. - The lowest common ancestor of a set
S
of nodes is the nodeA
with the largest depth such that every node in S is in the subtree with rootA
.
Example 1:
Input: root = [1,2,3]
Output: [1,2,3]
Explanation:
The deepest leaves are the nodes with values 2 and 3.
The lowest common ancestor of these leaves is the node with value 1.
The answer returned is a TreeNode object (not an array) with serialization "[1,2,3]".
Example 2:
Input: root = [1,2,3,4]
Output: [4]
Example 3:
Input: root = [1,2,3,4,5]
Output: [2,4,5]
思路:題目要求求deepest leaf的LCA,我們首先需要tree depth的信息(注意不是node depth, 也可以理解爲deepest leaf depth 也就是tree depth信息),然後跟LCA一樣,需要返回node信息,那麼我們就需要resultType作爲返回值;findLCA 表示當前枝,找到的LCA和它所能找到的deepest leaf 的depth;如果左右depth相等,證明當前node就是LCA;並返回leftnode的depth也就是deepest node的depth;
注意這裏有兩個表示:一個是method的depth代表node的depth,另外一個returnType裏面的depth代表找到的node的 deepest depth;
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
private class ReturnType {
public TreeNode node;
public int depth;
public ReturnType(TreeNode node, int depth) {
this.node = node;
this.depth = depth;
}
}
public TreeNode lcaDeepestLeaves(TreeNode root) {
if(root == null) {
return null;
}
ReturnType n = findLCA(root, 0);
return n.node;
}
private ReturnType findLCA(TreeNode root, int depth) {
if(root == null) {
return new ReturnType(null, depth);
}
ReturnType leftnode = findLCA(root.left, depth + 1);
ReturnType rightnode = findLCA(root.right, depth + 1);
if(leftnode.depth == rightnode.depth) {
return new ReturnType(root, leftnode.depth);
}
if(leftnode.depth > rightnode.depth) {
return leftnode;
} else {
return rightnode;
}
}
}